--BY Joel Mathias
Single Inheritance Program To Find The Volume Of A cylinder and a sphere
/* Program To Implement Simple Program On Single Inheritance */
class Sphere
{
double r;
double area()
{
return (r * r * r * 4 * 3.1416 / 3);
}
}
class Cylinder extends Sphere
{
double h;
double area()
{
return 3.1416 * r * r * h;
}
}
class Volume
{
public static void main(String args[])
{
Cylinder C = new Cylinder();
C.r = 2.0;
C.h = 6.0;
double cyl = C.area();
Sphere S = new Sphere();
S.r = 5.0;
double sph = S.area();
System.out.println("Area of sphere is "+sph+" and area of cylinder is "+cyl);
}
}
/* OUTPUT *
Area of sphere is 523.6 and area of cylinder is 75.3984 */
Labels: Core Java, Inheritance
Subscribe to:
Post Comments (Atom)
/* write a java program using multiple inheritance to find area of a rectangle and triangle */
class dc //base class
{
int a,l,b;
void set(int x,int y)
{
l=x;
b=y;
System.out.println("val of l="+l);
System.out.println("val of b="+b);
}
void areaOfRectangle()
{
a=l*b;
}
}//dc
class cd extends dc//derived class
{
int a,h,w;
void set(int x,int y)
{
super.set(x,y);
h=x;
w=y;
System.out.println("val of h="+h);
System.out.println("val of w="+w);
}
void areaOfTriangle()
{
super.areaOfRectangle();
System.out.println("area of rect="+super.a);
this.a=(h*w)/2;
System.out.println("area of triangle="+this.a);
}
}//cd
class idemo6
{
public static void main(String[] args)
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
cd c=new cd();
c.set(x,y);
c.areaOfTriangle();
}
}
thanx for the program...nicely done...