DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

Types of Inheritance in Java

What are the types of Inheritance?

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance

What is Single Inheritance?

  • One child class inherits from one parent class.
Parent
   ↑
 Child
Enter fullscreen mode Exit fullscreen mode

What is Multilevel Inheritance?

  • A class inherits from a class that already inherits another class.
    Grandparent  →  Parent  →  Child
Enter fullscreen mode Exit fullscreen mode

What is Hierarchical Inheritance?

  • Multiple child classes inherit from one parent class.
        Animal
        /    \
      Dog    Cat
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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.
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Top comments (0)