200k views
3 votes
Although saying "Cylinder IS A Circle" doesn't make sense, it can be implemented in Java using inheritance in a legitimate way. Implement Circle and Cylinder classes with fields, constructors and methods required to demonstrate all aspects of legitimate inheritance.

1 Answer

5 votes

Answer:

I've included in the codes the area to circle and volume to cylinder. The Volume method that was used, area() to compute, the volume of cylinder which shows that cylinder is an expansion of circle with an extra dimension (height)

Step-by-step explanation:

Temp is tester class

public class Temp {

public static void main(String[] args) {

Circle c = new Circle(7);

System.out.println("Area of circle with radius 7 is "+c.area());

Cylinder c1 = new Cylinder(7,10);

System.out.println("Cylinder volume with radius 7 and height 10: "+c1.volume());

}

}

----------------------------------------------------------------------------------------------------

class Circle{

int radius;

Circle(int r){

radius=r;

}

double area(){ //area of circle

return 3.14*radius*radius;

}

public int getRadius() {

return radius;

}

public void setRadius(int radius) {

this.radius = radius;

}

}

class Cylinder extends Circle{

int height;

Cylinder(int r, int h){

super(r);

height=h;

}

double volume(){ //volume of cylinder

return area()*height;

}

public int getHeight() { //accessor

return height;

}

public void setHeight(int height) { //mutator

this.height = height;

}

}

User Abellina
by
4.9k points