SlideShare a Scribd company logo
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 1/5
Inheritance
Inheritance
Inheritance: Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are built upon existing
classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Advantages of inheritance:
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
Syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
A class which is inherited is called a parent or superclass, and the new class is called child or subclass.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class,
extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also
called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields
and methods of the existing class when you create a new class. You can use the same fields and
methods already defined in the previous class.
Example:
Cat is the subclass and Animal is the superclass. The relationship between the two classes is Cat IS-A
Animal. It means that Cat is a type of Animal.
class Animal
{
}
class Cat extends Animal
{
}
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 2/5
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
typesofinheritance.jpg
In java programming, multiple and hybrid inheritance is supported through interface only.
multiple.jpg
Note: Multiple inheritance is not supported in Java through class.
When one class inherits multiple classes, it is known as multiple inheritance.
Single Inheritance Example
single.png
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Demo
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
barking...
eating...
Multilevel Inheritance Example
Multi level
inheritance.png
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 3/5
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class Demo
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
weeping...
barking...
eating...
Hierarchical Inheritance Example
heirarchical inheritance.png
class Animal
{
void eat()
{
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 4/5
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class Demo
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Output:
meowing...
eating...
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and
B classes have the same method and you call it from child class object, there will be ambiguity to call the
method of A or B class.
If you inherit 2 classes, there will be compile time error.
class A
{
void msg()
{
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 5/5
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B
{
public static void main(String args[])
{
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Output:
Compile Time Error

More Related Content

PDF
Interfaces in java
PDF
Classes, objects, methods, constructors, this keyword in java
PDF
Inheritance In Java
PPT
Inheritance in java
PPTX
Inheritance in Java
PPT
Java: Inheritance
PPTX
Inheritance ppt
PPTX
Inheritance in java
Interfaces in java
Classes, objects, methods, constructors, this keyword in java
Inheritance In Java
Inheritance in java
Inheritance in Java
Java: Inheritance
Inheritance ppt
Inheritance in java

What's hot (20)

PDF
java-06inheritance
PPTX
Multiple inheritance possible in Java
PPTX
Inheritance in Java
PPTX
Java inheritance
PDF
itft-Inheritance in java
PPT
Java inheritance
PPTX
Inheritance in JAVA PPT
PPTX
Inheritance in java
PPTX
Java Inheritance
PDF
javainheritance
PPTX
Inheritance In Java
PPTX
Inheritance and its types In Java
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
Inheritance polymorphism-in-java
PPTX
Java(inheritance)
PPT
Inheritance
PPT
Inheritance C#
PPTX
Java Inheritance | Java Course
PPTX
Inheritance and Polymorphism Java
java-06inheritance
Multiple inheritance possible in Java
Inheritance in Java
Java inheritance
itft-Inheritance in java
Java inheritance
Inheritance in JAVA PPT
Inheritance in java
Java Inheritance
javainheritance
Inheritance In Java
Inheritance and its types In Java
Interface in java ,multiple inheritance in java, interface implementation
Inheritance polymorphism-in-java
Java(inheritance)
Inheritance
Inheritance C#
Java Inheritance | Java Course
Inheritance and Polymorphism Java
Ad

Similar to Inheritance used in java (20)

PDF
Inheritance in Java.pdf
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
INHERITANCE.pptx
PPTX
PPTX
INHERTANCE , NARROW AND WIDENING
PPTX
Inheritance Interface and Packags in java programming.pptx
PPTX
Inheritance in Java beginner to advance with examples.pptx
PPTX
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
PPTX
Inheritance in java
PPTX
Java Inheritance
PPTX
Inheritance
PPTX
Inheritance in C++ (Programming Fundamentals)
PPTX
Inheritance in java
PPTX
Inheritance in java
PPTX
Inheritance,single,multiple.access rulepptx
PDF
java inheritance that is used in oop cls
PPTX
Basics to java programming and concepts of java
PPTX
inheritance.pptx
PPTX
Inheritance
PPT
web program-Inheritance,pack&except in Java.ppt
Inheritance in Java.pdf
Inheritance in Java is a mechanism in which one object acquires all the prope...
INHERITANCE.pptx
INHERTANCE , NARROW AND WIDENING
Inheritance Interface and Packags in java programming.pptx
Inheritance in Java beginner to advance with examples.pptx
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
Inheritance in java
Java Inheritance
Inheritance
Inheritance in C++ (Programming Fundamentals)
Inheritance in java
Inheritance in java
Inheritance,single,multiple.access rulepptx
java inheritance that is used in oop cls
Basics to java programming and concepts of java
inheritance.pptx
Inheritance
web program-Inheritance,pack&except in Java.ppt
Ad

More from TharuniDiddekunta (15)

PDF
String class
PDF
Exception handling basic
PDF
Creating your own exception
PDF
Built in exceptions
PDF
Packages access protection, importing packages
PDF
Operators, control statements represented in java
PDF
Arrays in java
PPTX
Software Metrics (Testing)
PPTX
unit 3 Design 1
PPTX
Unit 4 testing
PPTX
risk managment and quality
PPTX
PPT
Network layer
PPTX
Transport layer and Application layer
PPT
Congection control and Internet working
String class
Exception handling basic
Creating your own exception
Built in exceptions
Packages access protection, importing packages
Operators, control statements represented in java
Arrays in java
Software Metrics (Testing)
unit 3 Design 1
Unit 4 testing
risk managment and quality
Network layer
Transport layer and Application layer
Congection control and Internet working

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Geodesy 1.pptx...............................................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Well-logging-methods_new................
PPTX
Current and future trends in Computer Vision.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Artificial Intelligence
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
composite construction of structures.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Geodesy 1.pptx...............................................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CH1 Production IntroductoryConcepts.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Well-logging-methods_new................
Current and future trends in Computer Vision.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Artificial Intelligence
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
composite construction of structures.pdf

Inheritance used in java

  • 1. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 1/5 Inheritance Inheritance Inheritance: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. Advantages of inheritance: For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. Syntax: class Subclass-name extends Superclass-name { //methods and fields } A class which is inherited is called a parent or superclass, and the new class is called child or subclass. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. Example: Cat is the subclass and Animal is the superclass. The relationship between the two classes is Cat IS-A Animal. It means that Cat is a type of Animal. class Animal { } class Cat extends Animal { }
  • 2. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 2/5 Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. typesofinheritance.jpg In java programming, multiple and hybrid inheritance is supported through interface only. multiple.jpg Note: Multiple inheritance is not supported in Java through class. When one class inherits multiple classes, it is known as multiple inheritance. Single Inheritance Example single.png class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Demo { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); } } Output: barking... eating... Multilevel Inheritance Example Multi level inheritance.png
  • 3. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 3/5 class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } } class Demo { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } Output: weeping... barking... eating... Hierarchical Inheritance Example heirarchical inheritance.png class Animal { void eat() {
  • 4. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 4/5 System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class Demo { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error } } Output: meowing... eating... Why multiple inheritance is not supported in java? To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. If you inherit 2 classes, there will be compile time error. class A { void msg() {
  • 5. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 5/5 System.out.println("Hello"); } } class B { void msg() { System.out.println("Welcome"); } } class C extends A,B { public static void main(String args[]) { C obj=new C(); obj.msg();//Now which msg() method would be invoked? } } Output: Compile Time Error