Hybrid Inheritance in Java with Program Example

Hybrid inheritance is a type of inheritance in which two or more variations of inheritance are used. For instance, suppose, there are various classes namely A, B, C, D and E. if class A gets extended by class B, then this would be an example of Single inheritance. Further, if class C, D and E extend class B,then it would be an example of hierarchical inheritance. In this manner, multiple types of inheritance can be used within the same hierarchy structure, that is termed Hybrid inheritance.

Hybrid Inheritance in Java

 

Hybrid Inheritance Programs in Java

Hybrid inheritance can be implemented using combination of single, multilevel, and hierarchical inheritance. Below is a simple example for the same.

Example 1. Java program to implement Hybrid Inheritance with the help of single and hierarchical inheritance

Program:

public class A {
    public void display() {
        System.out.println("I am a method from class A");
    }
}

class B extends A {
    public void print() {
        System.out.println("I am a method from class B");
    }
}

class C extends B{
    public void show() {
        System.out.println("I am a method from class C");
    }
}

class D extends B{
    public void show() {
        System.out.println("I am a method from class D");
    }
}

class E extends B {
    public void outPut() {
        System.out.println("I am a method from class E");
    }

    public static void main(String[] args) {
        E objE = new E();
        objE.display(); // A is indirect parent of E, therefore, the display method gets inherited all the way to leaf
    }
}


Output:

I am a method from class A


Explanation:

  1. Class A and B are depicting the implementation of single inheritance as class B is inheriting class A where class A is a parent and class B is its single child
  2. Further, classes B, C, D and E are resembling the implementation of hierarchical inheritance as class B is being extended by C, D and E.
  3. Lastly, E has got access to class A’s method named display() indirectly through B. And we’ve used the display() method to print the output.

Example 2. Java program to implement Hybrid Inheritance with the help of multilevel and hierarchical inheritance

Program:

public class A {
    public void display() {
        System.out.println("I am a method from class A");
    }
}

class B extends A {
    public void print() {
        System.out.println("I am a method from class B");
    }
}

class C extends B {
    public void show() {
        System.out.println("I am a method from class C");
    }
}

class D extends C {
    public void show() {
        System.out.println("I am a method from class D");
    }
}

class E extends C {
    public void outPut() {
        System.out.println("I am a method from class E");
    }

    public static void main(String[] args) {
        E objE = new E();
        objE.display(); // A is indirect parent of E, therefore, the display method gets inherited all the way to leaf
    }
}

Output:

I am a method from class A


Explanation:

  1. In this example, classes A, B and C are depicting the implementation of multilevel inheritance as class A is being extended by class B and further, class B is being aquired by C.
  2. Further, classes C, D and E are providing the implementation of hierarchical inheritance as class C is being extended by D and E.
  3. Lastly, E can access class A’s method named display() through C and B. And we’ve used the method to print the output.

Leave a Reply