Java Program to add three numbers using single inheritance


In this program, You will learn how to add three numbers using single inheritance in Java.


class A { }

class B extends A { }


Example: How to add three numbers using single inheritance in Java

Run it Now
import java.util.Scanner;

class A {

    int a, b, c, sum;
    Scanner sc = new Scanner(System.in);

    void input() {
        System.out.println("Enter Three Numbers :");
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
    }
}

class Main extends A {

    void add() {
        sum = a + b + c;
    }

    void display() {
        System.out.println("The Sum is :" + sum);
    }

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

Output:

Enter Three Numbers :10 20 30
The Sum is :60