5.1: Program
on single Inheritance
import
java.io.*;
class Room
{
int length,
width;
Room(int x,int
y)
{
} length=x;
width=y;
int AreaO
{
Int a=length*
width;
Return a;
}
}
Class Child
extends Room
{
Int height;
Child(int x,int
y ,int z)
{
Super(x,y);
Height=z;
}
Int volumeO
{
Int v=length*
width* height;
Return v;
}
}
class
inherit
{ public static void main(String s[
])
{
Child obj= new Child(10,20,30);
int c= obj.Area();
int d= obj.Volume();
System.out.println(“Area=”+c);
System.out.println(“Volume=”+d);
}
}
Output
Area = 200
Volume =
6000
5.2. Program
on Multilevel Inheritance
Class A
{
Int length;
A(int x)
{
Length=x;
}
Int Area1()
{
Int
a=length*length;
Return a;
}
}
Class B
extends A
{
Int width;
B(int x, int y)
{
Super(x);
Width=y;
}
Int Area2()
{
Int r=length*width;
Return r;
}
}
Class C
extends B
{
Int height;
C(int x, int y,
int z)
{
Super(x,y);
Height=z;
}
Int volume()
{
Int
b=length*width*height;
Return b;
}
}
Class
Inherit
{
Public static void main(String s[])
{
C obj=new C (10,20, 30)
Int p= obj.Area1();
Int q = obj.Area2();
Int r = obj.volume();
System.out.println(“Area of
square is:”+p);
System.out.println(“Area of rectangle
is:”+q);
System.out.println(“Volume
is:”+r);
}
}
Output is
Area of square
is: 100
Area of
rectangle is: 200
Volume is:
6000
5.3: Program
on Hierarchical Inheritance
Class A
{
int length
A( int x)
{
Length=x;
}
int Areal ()
{
int a= length* length;
return a;
}
}
Class B
extends A
{
Int width;
B(int x, int y)
{
Super(x);
Width=y;
}
int Area2()
{
int r=
length * width;
return r;
}
}
Class C
extends A
{
int height;
C(int x, int y)
{
Super (x);
height=z;
}
int Area3()
{
int b= length * height
return b;
}
}
Class
inherit
{
Public static void main(string s[])
{
B obj1=new B (10,20);
Cobj2=new C (30,40);
Int p = obj1.Areal();
Int q = obj2.Areal();
Int r = obj1.Area1();
Int t = obj2.Area2();
System.out.println(“Areal is: ” +p);
System.out.println(“Areal is:”+q);
System.out.println(“Areal is:” +r);
System.out.println(“Areal is:” +t);
}
}
Output is
Areal is:
100
Area1 is:
900
Area2 is:
200
Area3 is:
1200
0 Comments
Please share your opinions and suggestions with us.