What is hybrid inheritance in java

What is hybrid inheritance in java:

Inheritance is an important part of object-oriented programming. A class can inherit features from another class. One is called the parent class and the other one is called the child class. The child class inherits properties from the parent class.

There are different types of inheritance in Java and hybrid inheritance is one of its types. In this post, we will learn how hybrid inheritance works in Java with examples.

What is Inheritance:

Suppose we have a few methods and properties defined in one class and we need to create one separate class that will use the same methods and properties. Instead of rewriting these, we can inherit these properties from the old class to the new class. Inheritance helps to achieve this.

These classes are in a parent-child relationship or Is-A relationship. The child class can reuse all the methods and properties defined in the parent class.

All classes in Java have a parent class. The Object class is the parent of all the classes.

Following are the categories of inheritance in Java:

  • Single inheritance
  • Multi-level inheritance
  • Hierarchical inheritance
  • Multiple inheritance
  • Hybrid inheritance

Hybrid inheritance:

Hybrid inheritance is achieved if a set of classes uses more than one inheritance. Let me show you different examples of Hybrid inheritance:

Example of Hybrid inheritance:

Let’s take a look at the below diagram: Hybrid inheritance example

Here, ClassB and ClassC are extending from ClassA. Also, ClassD is extending from ClassB and ClassC. It creates a hybrid inheritance.

But, multiple inheritance is not supported in Java. We can use interfaces to implement this.

class ClassA {
    public void printA() {
        System.out.println("Inside A");
    }
}

interface InterfaceB {
    public void printB();
}

interface InterfaceC {
    public void printC();
}

class ClassD extends ClassA implements InterfaceB, InterfaceC {

    public void printD() {
        System.out.println("Inside D");
    }

    @Override
    public void printB() {
        System.out.println("Inside B");
    }

    @Override
    public void printC() {
        System.out.println("Inside C");
    }
}

public class Main {
    public static void main(String[] args) {
        ClassD classD = new ClassD();
        classD.printA();
        classD.printB();
        classD.printC();
        classD.printD();
    }
}
  • We created two interfaces InterfaceB and InterfaceC.
  • The ClassD is extending ClassA and implements the interfaces.

If you run this program, it will print:

Inside A
Inside B
Inside C
Inside D

Example 2: Implementation of Hybrid inheritance using single and Hierarchical inheritances:

Let’s take another Hybrid inheritance example. We will use single and hierarchical inheritances to build the hybrid inheritance. The following diagram will help you to understand how it works:

Hybrid inheritance example

Here,

  • GrandFather and Father classes are single inheritance. Father is the child class and GrandFather is the parent class.
  • Son and Daughter classes are extending Father class. These three classes represents Hierarchical inheritance.
  • Combining all of these classes, a Hybrid inheritance is formed.

We can write it as below in code,

class GrandFather {
    public void print() {
        System.out.println("Inside GrandFather");
    }
}

class Father extends GrandFather {
    public void print() {
        System.out.println("Inside Father");
    }
}

class Son extends Father {
    public void print() {
        System.out.println("Inside Son");
    }
}

class Daughter extends Father {
    public void print() {
        System.out.println("Inside Daughter");
    }
}

Similar to the above two examples, we can combine different inheritances to build a hybrid inheritance.

You might also like: