Java program to show the concept of hierarchical inheritance

Hierarchical inheritance

It is a kind of inheritance in which one class is inherited by many sub classes.This program illustrate the concept of hierarchical inheritance.

import java.util.*;
class base
{
	int x,y;	
 Scanner kb=new Scanner(System.in);
public void getm()
{
   System.out.println("Values for addition:- ");
   System.out.println("Enter value of x:- ");
   x=kb.nextInt();
   System.out.println("Enter value of y:- ");
   y=kb.nextInt();
}

public void getb()
{
	System.out.println("\n......................................");
	System.out.println("\nValues for subtraction:- ");
   System.out.println("Enter value of x : ");
   x=kb.nextInt();
   System.out.println("Enter value of y: ");
   y=kb.nextInt();
}
}
class derived1 extends base
{
   int z;
	public void sum()
	{
      z=x+y;
    System.out.print("Sum of two numbers is: "+z);	  
	
	}
 
}

class derived2 extends base
{
	int a;
	public void sub()
	{
		a=x-y;
		System.out.print("\nSubtraction of two numbers is: "+a);
	}
}
class scan 

{
 public static void main(String arg[])
{
 derived1 d1=new derived1();
 derived2 d2=new derived2();
 d1.getm();
 d1.sum();
 d2.getb();
 d2.sub();

}
}

OUTPUT:-

hierarchical inheritance

Leave a Reply

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