Write a Java program to demonstrates the use of abstract classes in inheritance


This Java program demonstrates the use of abstract classes and method overriding to calculate the areas of shapes. It defines an abstract class Shape and two concrete subclasses Circle and Rectangle, which provide specific implementations for calculating the areas of circles and rectangles. Here's an explanation of the program:

  • public class Main: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is where the program execution starts. It creates instances of the Circle and Rectangle classes and calculates and prints their areas to the console.
  • Circle circle = new Circle(5);: This line creates an instance of the Circle class with a radius of 5.
  • Rectangle rectangle = new Rectangle(4, 6); : This line creates an instance of the Rectangle class with a length of 4 and a width of 6.
  • System.out.println("Area of Circle : " + circle.calculateArea());: It calculates and prints the area of the circle to the console using the calculateArea method.
  • System.out.println("Area of Rectangle : " + rectangle.calculateArea());: It calculates and prints the area of the rectangle to the console using the calculateArea method.
  • abstract class Shape: This is an abstract class that provides a blueprint for shape classes. It declares an abstract method calculateArea, which is to be implemented by concrete subclasses.
  • abstract double calculateArea(): The calculateArea method is declared as abstract, meaning that any subclass of Shape must provide a concrete implementation for this method. The method returns a double, representing the area of the shape.
  • class Circle extends Shape: This is the Circle class, which extends the Shape class.
  • double radius: The Circle class has a radius property to represent the radius of the circle.
  • Circle(double r): The constructor for the Circle class takes a radius parameter and initializes the radius property.
  • @Override double calculateArea(): The Circle class provides a concrete implementation of the calculateArea method, calculating the area of the circle using the formula π * radius^2.
  • class Rectangle extends Shape: This is the Rectangle class, which also extends the Shape class.
  • double length, width: The Rectangle class has length and width properties to represent the dimensions of the rectangle.
  • Rectangle(double l, double w): The constructor for the Rectangle class takes length and width parameters and initializes the corresponding properties.
  • @Override double calculateArea(): The Rectangle class provides a concrete implementation of the calculateArea method, calculating the area of the rectangle as the product of its length and width.

In this program, the Shape class serves as an abstract base class, defining the abstract calculateArea method that must be implemented by its concrete subclasses. Both the Circle and Rectangle classes provide specific implementations of the calculateArea method tailored to their respective shapes. The program demonstrates polymorphism by calling the calculateArea method on shape objects, which results in the appropriate area calculation for each shape.


Source Code

public class Main
{
	public static void main(String[] args)
	{
		Circle circle = new Circle(5);
		Rectangle rectangle = new Rectangle(4, 6);
 
		System.out.println("Area of Circle : " + circle.calculateArea());
		System.out.println("Area of Rectangle : " + rectangle.calculateArea());
	}
}
 
abstract class Shape
{
	abstract double calculateArea();
}
 
class Circle extends Shape
{
	double radius;
 
	Circle(double r)
	{
		radius = r;
	}
 
	@Override
	double calculateArea()
	{
		return Math.PI * radius * radius;
	}
}
 
class Rectangle extends Shape
{
	double length, width;
 
	Rectangle(double l, double w)
	{
		length = l;
		width = w;
	}
 
	@Override
	double calculateArea()
	{
		return length * width;
	}
}

Output

Area of Circle : 78.53981633974483
Area of Rectangle : 24.0