Hybrid Inheritance

Hybrid Inheritance


class Base {
    int a;
    Base() {
        a = 0;
    }
    Base(int x) {
        a = x;
    }
    void display() {
        System.out.println("\na = " + a);
    }
}
class Derived1 extends Base {
    int b;
    Derived1() {
        super(); // Calling Base class Default Constuctor
        b = 0;
    }
    Derived1(int x, int y) {
        super(x); // Calling Base class Parameterised Constructor
        b = y;
    }
    void display() {
        super.display(); // Calling Base class Display method
        System.out.println("b = " + b);
    }
}
interface B1 {
    int d = 40;
    void dispD();
    int e = 50;
    void dispE();
}
class Derived2 extends Derived1 implements B1 {
    int c;
    // Implementing interface methods of class B1
    public void dispD() {
        System.out.println("\nd = " + d);
    }
    public void dispE() {
        System.out.println("\ne = " + e);
    }
    Derived2() {
        super(); // Calling Derived1 class Default Constuctor
        c = 0;
    }
    Derived2(int x, int y, int z) {
        super(x, y); // Calling Derived1 class Parameterised Constructor
        c = z;
    }
    void display() {
        super.display(); // Calling Derived1 class Display method
        System.out.println("c = " + c);
    }
}
class HybridInheritance {
    public static void main(String[] args) {
        // Declaring Variables
        Derived2 d, d1;
        d = new Derived2();
        d.display();
        d1 = new Derived2(10, 20, 30);
        d1.display();
        d.dispD();
        d1.dispE();
    }
}

OUTPUT::

a = 0
b = 0
c = 0

a = 10
b = 20
c = 30

d = 40

e = 50


Previous Post Next Post