Single Inheritance – JAVA

When an only one class is derived from an existing base class is called single inheritance. Single inheritance is only possible if there are two classes which are inherit with each others. The new class is called DERIVED Class and an existing class is called BASE Class.

Syntax:

class base_class_name

{

…………….

…………….

}

class derived_class_name extends base_class_name

{

…………..

……………

}

 

//PROGRAM TO IMPLMENT SINGLE INHERITANCE

class Room

{

int length,breadth;

Room(int x,int y)                //PARMETERIZED CONSTRUCTOR

{

length=x;

breadth=y;

}

int area( )

{

return(length*breadth);

}

}

class DiningRoom extends Room

{

int height;

DiningRoom(int x,int y,int z)          //PARAMETERIZED CONSTRUCTOR

{

super(x,y);

height=z;

}

int volume( )

{

return(length*breadth*height);

}

}

class inheritTest

{

public static void main(String args[ ])

{

DiningRoom droom=new DiningRoom(14,12,10);

int d_area=droom.area( );

int d_volume=droom.volume( );

System.out.println(“Area of dining room =”+d_area);

System.out.println(“Volume of dining room =”+d_volume);

}

}

 

Leave a Reply

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