Abstract Class Shape Example in Java (Triangle, Rectangle, Circle)

Abstract Class Shape Example in Java (Triangle, Rectangle, Circle)

Understanding abstract classes in Java becomes much easier when you see them in action. One of the most common and practical examples used in exams and real-world programming is the Shape example using Triangle, Rectangle, and Circle.

In this guide, you’ll learn how abstract classes work, why they are used, and how different shapes can implement their own logic using the same base class.


What is an Abstract Class in Java?

An abstract class is a special type of class that cannot be instantiated directly. It acts as a blueprint for other classes.

It can contain:

  • Abstract methods (without body)
  • Concrete methods (with body)

This allows you to define a common structure while letting subclasses provide specific implementations.

Basic Abstract Class
abstract class Shape {
    abstract void area();
}
  

Why Use Abstract Classes?

Abstract classes are useful when multiple classes share a common structure but need different implementations.

  • Promotes code reuse
  • Ensures consistency across classes
  • Supports abstraction and polymorphism
  • Makes code easier to maintain

Abstract Class Shape Example (Triangle, Rectangle, Circle)

Let’s build a complete Java program where different shapes calculate their area using an abstract class.

Complete Java Program
abstract class Shape {
    abstract void area();
}

class Triangle extends Shape {
    double base = 5, height = 10;

    void area() {
        System.out.println("Triangle Area: " + (0.5 * base * height));
    }
}

class Rectangle extends Shape {
    double length = 8, width = 4;

    void area() {
        System.out.println("Rectangle Area: " + (length * width));
    }
}

class Circle extends Shape {
    double radius = 7;

    void area() {
        System.out.println("Circle Area: " + (3.14 * radius * radius));
    }
}

public class TestShape {
    public static void main(String[] args) {
        Shape s;

        s = new Triangle();
        s.area();

        s = new Rectangle();
        s.area();

        s = new Circle();
        s.area();
    }
}
  

Output

Program Output
Triangle Area: 25.0
Rectangle Area: 32.0
Circle Area: 153.86
  

How This Program Works

Here’s what’s happening step by step:

  • The abstract class Shape defines the method area()
  • Each subclass provides its own implementation
  • The reference variable Shape s enables runtime polymorphism
  • Java decides which method to call at runtime

This is a perfect example of abstraction + polymorphism working together.


Key Rules of Abstract Classes

  • Cannot create object of abstract class
  • Must be extended by subclass
  • Subclass must implement abstract methods
  • Can have constructors and normal methods

Abstract Class vs Interface

Feature Abstract Class Interface
Methods Abstract + Concrete Mostly Abstract
Variables Allowed Constants only
Constructor Yes No

Real-World Applications

  • UI component systems
  • Game object hierarchies
  • Banking systems
  • Framework architecture

Common Mistakes to Avoid

  • Trying to instantiate abstract class
  • Not implementing abstract methods
  • Using abstract class instead of interface incorrectly

Best Practices

  • Use meaningful method names
  • Keep abstraction simple
  • Use abstract class when sharing common logic

FAQ

What is an abstract class in Java?

It is a class that cannot be instantiated and is used as a base for other classes.

Can abstract class have methods with body?

Yes, it can have both abstract and non-abstract methods.

Why use abstract class?

To provide a common structure while allowing different implementations.


Conclusion

The abstract class Shape example is one of the best ways to understand Java abstraction. It shows how multiple classes can share a common base while implementing their own logic.

Practice this example and try modifying it to build your understanding further.


📚 Read More

Comments