Hierarchical Inheritance: In this type of inheritance, we create multiple derived classes on the basis of the single base class.
PROGRAM
class A
{
private int a;
public void
setA(int i)
{
a=i;
}
public void
showA( )
{
System.out.println("a="+a);
}
}
class B extends A
{
private int b;
public void
setB(int j)
{
b=j;
}
public void
showB( )
{
System.out.println("b="+b);
}
}
class C extends A
{
private int c;
public void
setC(int k)
{
c=k;
}
public void
showC( )
{
System.out.println("c="+c);
}
}
class HLevel
{
public static
void main(String args[ ])
{
B ob1=new B( );
ob1.setA(5);
ob1.setB(10);
ob1.showA();
ob1.showB();
C ob2=new C( );
ob2.setA(19);
ob2.setC(15);
ob2.showA();
ob2.showC();
}
}
Comments
Post a Comment