Multilevel Inheritance

Multilevel 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);
    }
}
class Derived2 extends Derived1 {
    int c;
    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 MultiLevelInheritance {
    public static void main(String[] args) {
        Derived2 d, d1; // Declaring Variables
        d = new Derived2();
        d.display();
        d1 = new Derived2(10, 20, 30);
        d1.display();
    }
}

OUTPUT::


a = 0
b = 0
c = 0

a = 10
b = 20
c = 30


Previous Post Next Post