What are the types of Inheritance?
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
What is Single Inheritance?
- One child class inherits from one parent class.
Parent
↑
Child
What is Multilevel Inheritance?
- A class inherits from a class that already inherits another class.
Grandparent → Parent → Child
What is Hierarchical Inheritance?
- Multiple child classes inherit from one parent class.
Animal
/ \
Dog Cat
What is Hybrid Inheritance?
- A hybrid inheritance is a combination of more than one types of inheritance.
What is Multiple Inheritance?
- Class inheritance from more than one parent class. However, Java does not support multiple inheritance of classes to avoid ambiguity issues like the "diamond problem".
What is Diamond Problem?
- This problem occurs when a class (Class D) inherits from two other classes (Class B and Class C), and both of those classes inherit from a common superclass (Class A). If a method defined in Class A is overridden by both Class B and Class C, the compiler cannot determine which specific overridden method to call when it is invoked from an object of Class D, leading to ambiguity.
A
/ \
B C
\ /
D
- B and C inherit from A
- D inherits from both B and C
- Now confusion happens if both B and C override the same method.
class A {
void show() {
System.out.println("From A");
}
}
class B extends A {
void show() {
System.out.println("From B");
}
}
class C extends A {
void show() {
System.out.println("From C");
}
}
// Java does NOT allow this
class D extends B, C {
}
Now if we do
D obj = new D();
obj.show();
Should it call:
show() from B?
OR show() from C?
This confusion is the Diamond Problem.
Example for Single Inheritance:
//Parent Class
public class Animal {
int id;
void eat()
{
System.out.println("This is parent class.Animal is eating.");
}
}
//Child Class
public class Dog extends Animal{
void bark()
{
System.out.println("This is child class. Dog is barking");
}
public static void main(String[] args)
{
Dog dg = new Dog();
dg.id = 10;
dg.eat();
dg.bark();
System.out.println(dg.id);
}
}
Output :
This is parent class.Animal is eating.
This is child class. Dog is barking
10
Example for Hierarchical Inheritance:
public class Animal {
int id;
void eat()
{
System.out.println("This is parent class.Animal is eating.");
}
}
public class Dog extends Animal{
void bark()
{
System.out.println("This is child class. Dog is barking");
}
public static void main(String[] args)
{
Dog dg = new Dog();
dg.id = 10;
dg.eat();
dg.bark();
System.out.println(dg.id);
}
}
public class Cat extends Animal{
void meow(){
System.out.println("cat is meowing");
}
public static void main(String[] args)
{
Cat ct = new Cat();
ct.eat();
ct.meow();
}
}
Output :
This is parent class.Animal is eating.
cat is meowing
Example for Multilevel Inheritance:
public class Animal {
int id;
void eat()
{
System.out.println("This is parent class.Animal is eating.");
}
}
public class Dog extends Animal{
void bark()
{
System.out.println("This is child class. Dog is barking");
}
public static void main(String[] args)
{
Dog dg = new Dog();
dg.id = 10;
dg.eat();
dg.bark();
System.out.println(dg.id);
}
}
public class Puppy extends Dog{
void weep(){
System.out.println("Puppy is weeping");
}
public static void main(String[] args)
{
Puppy pp = new Puppy();
pp.eat(); //method from class Animal
pp.bark(); //method from class Dog
pp.weep(); //method from class Puppy
}
}
Output:
This is parent class.Animal is eating.
This is child class. Dog is barking
Puppy is weeping
Top comments (0)