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 { 
   //statement
}

class B extends A { 
   //statement
}

Example: How to add three numbers using single inheritance in java.

import java.util.Scanner;

class A {

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

    void input() {
        System.out.print("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("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
Sum is:60