Java Program to multilevel inheritance with 4 levels of hierarchy


In this program, You will learn how to implement multilevel inheritance with 4 levels of hierarchy in Java.


Scanner sc = new Scanner(System.in);


Example: How to implement multilevel inheritance with 4 levels of hierarchy in Java

Run it Now
import java.util.Scanner;

class A {

    int a;
}

class B extends A {

    Scanner sc = new Scanner(System.in);
}

class C extends B {

    void input() {
        System.out.print("Enter a number: ");
        a = sc.nextInt();
    }
}

class Main extends C {

    void display() {
        System.out.println("\nThe value of a is: " + a);
    }

    public static void main(String args[]) {
        Main obj = new Main();
        obj.input();
        obj.display();
    }
}

Output:

Enter a number: 12
The value of a is: 12