Abstract Classes (Introduction)
Abstract Classes (Introduction)
So far every class you have written can be instantiated — you can call new Dog(), new Circle(), and so on. But sometimes a class exists purely as a blueprint that other classes must fill in. It makes no sense to create a bare Shape object if you never intend to draw it directly. Java gives you a precise tool for this: the abstract class.
What Does abstract Mean?
Adding the abstract keyword to a class declaration tells the compiler two things:
- This class cannot be instantiated directly. Calling
new Shape()is a compile-time error. - It may contain abstract methods — method signatures with no body — that subclasses are required to implement.
Declaring an Abstract Class and Abstract Methods
Here is the syntax:
The method area() has no body — just a semicolon after the signature. Every concrete subclass must override it, or the compiler refuses to compile that subclass.
Creating Concrete Subclasses
A concrete class is simply one that is not abstract. It provides real implementations for all abstract methods it inherits:
Now you can use them:
Shape references. The call to describe() is defined once in the abstract class, yet it internally calls the correct area() implementation at runtime — that is dynamic dispatch from Lesson 5 in action.
Providing a Partial Implementation
One of the greatest strengths of abstract classes is that they can do some of the work for subclasses. Suppose every shape has a colour that you track the same way for all of them:
The colour field, its getter, and the describe() method are written once and shared. Only the part that truly varies — area() — is left for each subclass to define.
Rules to Remember
- A class with at least one abstract method must be declared abstract.
- An abstract class may have zero abstract methods — it is still un-instantiable, which can be useful on its own.
- A subclass that does not implement all inherited abstract methods must itself be declared abstract.
- Abstract classes can have constructors, fields, and static members — they just cannot be instantiated directly.
Abstract Classes vs Interfaces — a First Glance
You will learn about interfaces soon. For now, the short distinction is:
- An abstract class can have fields, constructors, and both abstract and concrete methods. Use it when subclasses share state or common behaviour.
- An interface defines a pure contract — no state. Use it when you want to describe a capability that unrelated classes can share.
Summary
Abstract classes sit between a fully concrete class and a pure interface. They let you define what subclasses must do (abstract methods) while also providing shared implementation (concrete methods and fields). The abstract keyword on the class prevents accidental instantiation, and the abstract keyword on a method forces every concrete subclass to supply a real body. This pattern is the foundation of the shape hierarchy you will build in the final lesson of this tutorial.