Java program to sum of two numbers in single level inheritance

Sum of two numbers

This program shows how to add two numbers using the concept single level inheritance.We have made two class.In first class i.e. “base” user will input a number using function “get()” and in second class i.e “scan” which extends the “base” class operation on two numbers will be performed using function “sum()” and result will be displayed using function “display()”. The all three functions will be called by object of “scan” class i.e. “d1”.

import java.util.*;
class base
{
int x,y;
public void get()
{
 Scanner kb=new Scanner(System.in);
 System.out.println("Enter first number:- ");
 y=kb.nextInt();
 System.out.println("Enter Second number:- ");
 x=kb.nextInt();
}
}
class scan extends base
{
	int z;
	public void sum()
	{
		z=x+y;
    }
 
 public void display()
 {
	 System.out.println("Sum of two numbers is:- "+z);
 }
 
 public static void main(String arg[])
{
 scan d1=new scan();
 d1.get();
 d1.sum();
 d1.display();
}
}

OUTPUT:-

sum of two numbers

Leave a Reply

Your email address will not be published. Required fields are marked *