Thursday, 31 May 2012

Inheritance in Java example 1

Inheritance in Java example 1 

class box1
{
int length,breadth;
box1(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return length*breadth;
}
}
class box2 extends box1
{
int height;
box2(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return length*breadth*height;
}
}
class boxes
{
public static void main(String args[])
{
box2 ob=new box2(10,10,10);
int area=ob.area();
int volume=ob.volume();
System.out.println("Area is "+area);
System.out.println("Volume is "+volume);
}
}


No comments:

Post a Comment