Abstract class in java and why it is needed

In Java, an abstract class is a class that cannot be instantiated directly and is typically used as a blueprint for other classes. It allows you to define common methods and fields that can be inherited by its subclasses, while also providing the flexibility to declare certain methods as abstract, which means they have no implementation in the abstract class itself and must be implemented by its subclasses. Abstract classes are needed to promote code reuse, enforce design contracts, and enable polymorphic behavior.

Here’s an example to illustrate the concept of an abstract class and why it is needed:

// Abstract class representing a generic shape
abstract class Shape {
    // Fields common to all shapes
    protected String color;    
    // Constructor
    public Shape(String color) {
        this.color = color;
    }    
    // Abstract method to calculate area (to be implemented by subclasses)
    public abstract double calculateArea();    
    // Concrete method to display the color
    public void displayColor() {
        System.out.println("This shape is " + color);
    }
}
// Concrete subclass representing a rectangle
class Rectangle extends Shape {
    private double width;
    private double height;    
    // Constructor
    public Rectangle(String color, double width, double height) {
        super(color);
        this.width = width;
        this.height = height;
    }    
    // Implementation of abstract method for calculating area
    @Override
    public double calculateArea() {
        return width * height;
    }
}
// Concrete subclass representing a circle
class Circle extends Shape {
    private double radius;    
    // Constructor
    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }    
    // Implementation of abstract method for calculating area
    @Override
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating objects of concrete subclasses
        Rectangle rectangle = new Rectangle("Blue", 5, 4);
        Circle circle = new Circle("Red", 3);
        
        // Calling methods on the objects
        rectangle.displayColor();
        System.out.println("Area of Rectangle: " + rectangle.calculateArea());
        
        circle.displayColor();
        System.out.println("Area of Circle: " + circle.calculateArea());
    }
}

In this example:

  • Shape is an abstract class representing a generic shape. It defines a constructor, a concrete method displayColor(), and an abstract method calculateArea().
  • Rectangle and Circle are concrete subclasses of Shape that provide implementations for the calculateArea() method specific to their respective shapes.
  • Abstract class Shape provides a common structure for different shapes, allowing for code reuse and promoting consistency in behavior.
  • Subclasses Rectangle and Circle inherit common functionality from the abstract class Shape while providing specialized implementations for their specific shapes.

In summary, abstract classes in Java are needed to define common structure and behavior for a group of related classes, while also allowing for flexibility in implementation through abstract methods. They promote code reuse, enforce design contracts, and facilitate polymorphic behavior, making them a valuable tool in object-oriented programming.

Leave a comment