on 2 comments

Hybrid Inheritance in Java with Example

We know that a class can inherits functionality of other class called inheritance. Hybrid inheritance is a type of inheritance. Hybrid inheritance is combination of both single inheritance and multiple inheritance. In java, we cannot implement multiple inheritance using classes. For example, java compiler shows error, if we write class A extends B, C.

We can achieve hybrid inheritance through interfaces. Because multiple inheritance can be achieve by interfaces.

Hybrid Inheritance in Java
Hybrid Inheritance in Java

Example of Hybrid Inheritance in Java

In this program, interface B & C extends interface A and class HybridInheritance implements interface B, C.

interface A {
    public void methodA();
}
interface B extends A {
    public void methodB();
}
interface C extends A {
    public void methodC();
}
class HybridInheritance implements B, C {
    public void methodA() {
        System.out.println("Calling methodA");
    
    }
    public void methodB() {
        System.out.println("Calling methodB");
    }
    public void methodC() {
        System.out.println("Calling methodC");
    }
    public static void main(String args[]) {
        HybridInheritance obj = new HybridInheritance();
        obj.methodA();
        obj.methodB();
        obj.methodC();
    }
}

Output:

Calling methodA
Calling methodB
Calling methodC


2 comments:

  1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    java training in chennai

    ReplyDelete
  2. thankyou for this program aka souce code.

    ReplyDelete