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.

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("The value of a is:" + a);
    }

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

Output:

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