Program of single inheritance in Java

Free Java Programs

Program of single inheritance

class Room
{
int length;
int breadth;
Room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class BedRoom extends Rom // inheriting Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y); // pass values to superclass
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class InherTest
{
public static void main(String args[])
{
BedRoom room1=new BedRoom(14,12,10);
int area1=room1.area(); //superclass method
int volume1=room1.volume(); //baseclass method
System.out.println(“Area1=”+area1);
System.out.println(“Volume1=”+Volume1);
}
}

Output is:
Area1=168
Volume1=1680

Post a Comment

Previous Post Next Post