Java

Calculating the Area of a Circle and Cylinder using the ‘super’ Keyword in Java1 min read

Java program that calculates the area of a cylinder and a circle using the super keyword:

In this program, the Shape class is the base class with a single instance variable radius. The Circle class extends Shape and provides its own implementation of the area method to calculate the area of a circle. The Cylinder class extends Shape as well and adds an additional instance variable height. It also provides its own implementation of the area method to calculate the surface area of a cylinder.




In the main method of the Test class, objects of both the Circle and Cylinder classes are created and their areas are calculated using the area method. The result is printed to the screen. The super keyword is used in the constructors of both the Circle and Cylinder classes to call the constructor of the parent Shape class and initialize the radius instance variable.

Leave a Comment