Java Program to single inheritance using the Scanner class


In this program, You will learn how to implement single inheritance using the Scanner class in Java.


Scanner sc = new Scanner(System.in);


Example: How to implement single inheritance using the Scanner class in Java

Run it Now
import java.util.Scanner;

class A {

    int a;
    Scanner sc = new Scanner(System.in);

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

class Main extends A {

    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