Hierarchical Inheritance :

source file name:  Hierarchicaldemo.java

class A 

void sqcube(int n) 

int sq=n*n; 

int c=n*n*n; 

System.out.println("square="+sq+" cube="+c); 

class B extends A 

void add(int a,int b) 

System.out.println("sum="+(a+b)); 

}

void sub(int a,int b) 

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

class C extends A 

 

void fact(int n) 

int f=1; 

for(;n>0;n--) 

f=f*n; 

System.out.println("factorial value="+f); } 


class Hierarchicaldemo 

public static void main(String as[])

 { 

System.out.println("Inheritance program");

B b1=new B(); 

C c1=new C(); 

b1.sub(150,25); 

b1.add(50,550); 

c1.fact(5); 

c1.sqcube(6); 

b1.sqcube(9); 

}

output:


D:\JavaPrograms\InheritancePrgs>javac Hierarchicaldemo.java

D:\JavaPrograms\InheritancePrgs>java Hierarchicaldemo
Inheritance program
substract=125
sum=600
factorial value=120
square=36 cube=216
square=81 cube=729