Multiple Inheritance

Multiple Inheritance


interface Base1 {
    int a = 10;
    void dispB1();
}
interface Base2 {
    int b = 20;
    void dispB2();
}
// Note interfaces are implemented not extended
class Derived implements Base1, Base2 {
    // Implementing interface methods of class Base1
    public void dispB1() {
        System.out.println("a = " + a);
    }
    // Implementing interface methods of class Base2
    public void dispB2() {
        System.out.println("b = " + b);
    }
    int c;
    Derived() {
        c = 0;
    }
    Derived(int x) {
        c = x;
    }
    void display() {
        System.out.println("\nc = " + c);
    }
}
class MultipleInheritance {
    public static void main(String[] args) {
        Derived d, d1; // Declaring Variables
        d = new Derived();
        d.display();
        d.dispB1();
        d.dispB2();
        d1 = new Derived(30);
        d1.display();
    }
}


OUTPUT::

c = 0
a = 10
b = 20

c = 30

Previous Post Next Post