// Abstract class Shape abstract class Shape { abstract double area(); } // Circle class extending Shape class Circle extends Shape { private double radius; // Constructor public Circle(double radius) { this.radius = radius; //old variable = new variable } double area() { return Math.PI * radius * radius; // Area of Circle = πr² } } class Sphere extends Shape { private double radius; public Sphere(double radius) { this.radius = radius; } double area() { return 4 * Math.PI * radius * radius; // Surface Area of Sphere = 4πr² } } public class ShapeTest { public static void main(String[] args) { Circle circle = new Circle(5); Sphere sphere = new Sphere(5); System.out.println("Circle Area: " + circle.area()); System.out.println("Sphere Area: " + sphere.area()); } }
Wednesday, 5 February 2025
Define an abstract class Shape with an abstract method area(). Write a Java program to calculate the area of a circle and a sphere
22:40