Write a program to add and subtract two numbers. The program should illustrate the use of Inheritance.

import java.io.*;
class Add
{
	int num1;
	int num2;
	int AddNum()
	{
		int sum=0;
		sum=num1+num2;
		return sum;                    //Returns the sum of num1 and num2
	}
}
class Sub extends Add
{
	int subNum()
	{
		int res=0;
		res=num1-num2;
		return res;                    //Returns the difference of num1 and num2
	}
}
public class InheritanceDisplay
{
	public static void main(String args[])
	{
		Sub sobj=new Sub();
		sobj.num1=10;
		sobj.num2=5;
		int a=sobj.addNum();                               //calling addNum()function
		int b=sobj.subNum();                               //calling subNum()function
		System.out.println("\nNumber1="+sobj.mun1);       //Displays 10
		System.out.println("\nNumber2="+sobj.num2);       //Displays 5
		System.out.println("\nSum="+a);                   //Displays the sum returned by addNum()function
		System.out.println("\nDifference="+b);            //Displays the difference returned by subNum()function
	}
}

Leave a Reply

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

Related Posts

Show Buttons
Hide Buttons