ISC Practicals and Theory - Plane to Circle/Cylinder (OOP)
Updated 21 Oct 2026
A cylinder has a circle as its base, so a base class Plane holds the radius and the area of the circle, and a derived class Cylinder adds a height and finds the volume and surface areas by reusing the inherited radius and area(). This version uses parameterised constructors with super(...), overrides display() in Cylinder, and lets main hold a Plane reference to a Cylinder object, so the Cylinder version of display() runs.
Definition
Inheritance lets a derived class (class Cylinder extends Plane) use the data members and methods of the base class and add its own. A derived class constructor passes values to the base class constructor with super(...), which must be its first statement. A method written again in the derived class with the same name and parameters is redefined (overridden). When a base class reference holds a derived class object, the version that runs is chosen by the object, not by the reference type: subclass polymorphism with dynamic binding.
For a circle of radius $r$ and a cylinder of height $h$:
- area of the circle $= \pi r^2$
- volume $= \pi r^2 h$ (base area $\times$ height)
- curved surface area $= 2 \pi r h$
- total surface area $= 2 \pi r h + 2 \pi r^2$ (curved area plus two circular ends)
The program uses $\pi = 3.14159$ as a constant.
For example, $r = 3$ and $h = 5$: base area $= 28.27431$, volume $= 141.37155$, curved area $= 94.2477$, total area $= 94.2477 + 2 \times 28.27431 = 150.79632$.
Both radius and height must be greater than 0; otherwise the program prints INVALID INPUT.
Algorithm
main(in the driver classTest) readsr. If it is 0 or less,mainprintsINVALID INPUTand stops.mainreadsh. If it is 0 or less,mainprintsINVALID INPUTand stops.maincreates the object withPlane ob = new Cylinder(r, h). TheCylinderconstructor callssuper(r), which setsradius, and then setsheight.maincallsob.display(). Because the object is aCylinder, the overridingdisplay()ofCylinderruns:super.display()prints the radius and the base area, usingarea()ofPlane(PI * radius * radius);- it prints the height;
volume()returnsarea() * height, reusing the inherited method;curvedArea()returns2 * PI * radius * height;totalArea()returnscurvedArea() + 2 * area().
- Every result is printed to 2 decimal places with
printf("%.2f").
Program
Plane holds the radius and the circle's area, Cylinder adds the height and the three cylinder results, and Test holds main. Note that volume(), curvedArea() and totalArea() are called inside Cylinder's own display(), because a Plane reference can only call methods that Plane has.
import java.util.Scanner;
/* Base class: a plane figure, the circle, described by its radius. */
class Plane
{
protected double radius; // radius of the circle
protected final double PI = 3.14159; // value of pi used in all formulae
/* Parameterised constructor. r: radius of the circle. */
Plane(double r)
{
radius = r;
}
/* Returns the area of the circle: PI * r * r. */
double area()
{
return PI * radius * radius;
}
/* Prints the radius and the area of the circle. */
void display()
{
System.out.printf("Radius : %.2f%n", radius);
System.out.printf("Area of the base : %.2f%n", area());
}
}
/* Derived class: a cylinder, a Plane (its base) with a height. */
class Cylinder extends Plane
{
double height; // height of the cylinder
/* Parameterised constructor: r goes to the base class, h is the height. */
Cylinder(double r, double h)
{
super(r); // base class constructor sets the radius
height = h;
}
/* Returns the volume: base area * height (area() is inherited). */
double volume()
{
return area() * height;
}
/* Returns the curved surface area: 2 * PI * r * h. */
double curvedArea()
{
return 2 * PI * radius * height;
}
/* Returns the total surface area: curved area + two circular ends. */
double totalArea()
{
return curvedArea() + 2 * area();
}
/* Overrides display(): base class details first, then the cylinder's results. */
void display()
{
super.display();
System.out.printf("Height : %.2f%n", height);
System.out.printf("Volume : %.2f%n", volume());
System.out.printf("Curved surface area: %.2f%n", curvedArea());
System.out.printf("Total surface area : %.2f%n", totalArea());
}
}
/* Driver class: holds main. */
public class Test
{
/* Reads radius and height, checks them, and prints through a Plane reference. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the input
double r, h; // radius and height
System.out.print("Enter radius: ");
r = sc.nextDouble();
if (r <= 0)
{
System.out.println("INVALID INPUT");
return;
}
System.out.print("Enter height: ");
h = sc.nextDouble();
if (h <= 0)
{
System.out.println("INVALID INPUT");
return;
}
Plane ob = new Cylinder(r, h); // base reference, derived object
ob.display(); // runs Cylinder's display(), because the object is a Cylinder
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
radius | double | Protected data member of Plane: radius of the circle and of the cylinder's base |
PI | double | Protected constant of Plane: value of pi (3.14159) |
r | double | Parameter of both constructors: radius; also the local in main that holds the typed radius |
height | double | Data member of Cylinder: height of the cylinder |
h | double | Parameter of the Cylinder constructor: height; also the local in main that holds the typed height |
sc | Scanner | Local in main: reads the input |
ob | Plane | Reference of the base class type in main, holding the Cylinder object |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter radius: 3
Enter height: 5
Radius : 3.00
Area of the base : 28.27
Height : 5.00
Volume : 141.37
Curved surface area: 94.25
Total surface area : 150.80Sample 2
Enter radius: 0.1
Enter height: 0.2
Radius : 0.10
Area of the base : 0.03
Height : 0.20
Volume : 0.01
Curved surface area: 0.13
Total surface area : 0.19Sample 3
Enter radius: 2.5
Enter height: 10
Radius : 2.50
Area of the base : 19.63
Height : 10.00
Volume : 196.35
Curved surface area: 157.08
Total surface area : 196.35Sample 4
Enter radius: 4
Enter height: -2
INVALID INPUTDry run
Take the first sample: r $= 3$, h $= 5$. main creates new Cylinder(3.0, 5.0), so radius is 3.0 and height is 5.0. display() of Cylinder works out:
| Step | Statement | Value |
|---|---|---|
| 1 | area(): 3.14159 * 3 * 3 | 28.27431 |
| 2 | volume(): area() * 5 | 141.37155 |
| 3 | curvedArea(): 2 * 3.14159 * 3 * 5 | 94.2477 |
| 4 | totalArea(): 94.2477 + 2 * 28.27431 | 150.79632 |
Printed to 2 decimal places these are 28.27, 141.37, 94.25 and 150.80, so the last line is Total surface area : 150.80, as in the first sample run.
Variations
- Cone "Write a base class for a circle and a derived class for a cone that finds its volume, curved surface area and total surface area."
- Sphere "Derive a class for a sphere from the circle class and find its volume and surface area."
- Abstract base class "Make the base class abstract with an abstract method for the area and give its body in the derived class."