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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.