Documente Academic
Documente Profesional
Documente Cultură
In this exercise, a subclass called Cylinder is derived from the superclass Circle as
shown in the class diagram (where an an arrow pointing up from the subclass to its
superclass). Study how the subclass Cylinder invokes the superclass' constructors
(via super() and super(radius)) and inherits the variables and methods from the
superclass Circle.
/*
* The Circle class models a circle with a radius and color.
*/
class Circle
{
// private instance variable, not accessible from outside this class
private double radius;
private String color;
// The default constructor with no argument.
// It sets the radius and color to their default value.
public Circle()
{
radius = 1.0;
color = "red";
}
Output:
Cylinder: subclass of Circle[radius=1.0 color=red] height=1.0
Cylinder: radius=1.0 height=1.0 base area=3.141592653589793
volume=3.141592653589793
Cylinder: subclass of Circle[radius=1.0 color=red] height=10.0
Cylinder: radius=1.0 height=10.0 base area=3.141592653589793
volume=31.41592653589793
Cylinder: subclass of Circle[radius=2.0 color=red] height=10.0
Cylinder: radius=2.0 height=10.0 base area=12.566370614359172
volume=125.66370614359172