0% found this document useful (0 votes)
56 views2 pages

Activity 2 PDF

This document discusses inheritance concepts using a Circle and Cylinder class example. It explains that the Cylinder subclass inherits from the Circle superclass, and overrides the getArea() method to return the surface area of a cylinder instead of the base area. It notes that overriding getArea() breaks the getVolume() method, so getVolume() needs to be fixed. It suggests invoking the superclass Circle's getArea() using super.getArea() within the overridden getArea() method. Finally, it prompts adding a toString() method to the Cylinder class to override the one inherited from Circle.

Uploaded by

Guia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

Activity 2 PDF

This document discusses inheritance concepts using a Circle and Cylinder class example. It explains that the Cylinder subclass inherits from the Circle superclass, and overrides the getArea() method to return the surface area of a cylinder instead of the base area. It notes that overriding getArea() breaks the getVolume() method, so getVolume() needs to be fixed. It suggests invoking the superclass Circle's getArea() using super.getArea() within the overridden getArea() method. Finally, it prompts adding a toString() method to the Cylinder class to override the one inherited from Circle.

Uploaded by

Guia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercises on Inheritance

The Circle and Cylinder Classes

This exercise shall guide you through the important concepts in inheritance.

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.
You can reuse the Circle class that you have created in the previous exercise. Make sure that
you keep "Circle.class" in the same directory.
Write a test program (says TestCylinder) to test the Cylinder class created.
Method Overriding and " Super" :
The subclass Cylinder inherits getArea() method from its superclass Circle. Try overriding the
getArea() method in the subclass Cylinder to compute the surface area (=2π×radius×height +
2×base-area) of the cylinder instead of base area. That is, if getArea() is called by a Circle
instance, it returns the area. If getArea() is called by a Cylinder instance, it returns the surface
area of the cylinder.

If you override the getArea() in the subclass Cylinder, the getVolume() no longer works. This is
because the getVolume() uses the overridden getArea() method found in the same class. (Java
runtime will search the superclass only if it cannot locate the method in this class). Fix the
getVolume().

Hints: After overridding the getArea() in subclass Cylinder, you can choose to invoke the
getArea() of the superclass Circle by calling super.getArea().

TRY:
Provide a toString() method to the Cylinder class, which overrides the toString() inherited from
the superclass Circle.
Try out the toString() method in TestCylinder.

You might also like