Write a program in java to demonstrate in inheritance?

import java.util.Scanner;

class P

{

                int a,b;

                public void add(int x,int y)

                {

                                a=x;

                                b=y;

                                System.out.println("Addition "+(a+b));

                }

}

class C extends P

{

                public void sub(int x,int y)

                {

                                a=x;

                                b=y;

                                System.out.println("Subtraction "+(a-b));

                }

                public static void main(String arg[])

                {

                                Scanner input=new Scanner(System.in);

                                System.out.print("Enter First Number ");

                                int x=input.nextInt();

                                System.out.print("Enter Second Number ");

                                int y=input.nextInt();

                                C object=new C();

                                object.add(x,y);

                                object.sub(x,y);

                }

}

Example of Inheritance (Demonstrate in Inheritance).PNG

, , , , , , ,

Leave a Reply