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.

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

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

Output:

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