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;
		return sum;                    //Returns the sum of num1 and num2
class Sub extends Add
	int subNum()
		int res=0;
		return res;                    //Returns the difference of num1 and num2
public class InheritanceDisplay
	public static void main(String args[])
		Sub sobj=new Sub();
		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