Abstract class in Java is the blueprint for building hierarchical, reusable code. It allows us to define methods that subclasses must implement while sharing common logic. In this guide, we’ll learn how to declare abstract classes, override abstract methods, and leverage them for efficient object-oriented design.
Table of Contents
What is an Abstract Class in Java?
An abstract class is a class declared with the abstract
keyword that:
- Cannot be instantiated directly (no
new AbstractClass()
). - Can have abstract methods (no implementation) and concrete methods (with implementation).
- Forces subclasses to implement abstract methods (unless the subclass is also abstract).
When to Use an Abstract Class?
- Share Common Code: Provide reusable methods across related subclasses.
- Enforce Method Implementation: Define a contract for subclasses via abstract methods.
- Partial Implementation: Combine implemented and unimplemented methods.
Syntax of an Abstract Class
abstract class Animal {
// Abstract method (no body)
abstract void sound();
// Concrete method
void eat() {
System.out.println("Eating...");
}
}
Step-by-Step Example: Abstract Class in Action
1. Define the Abstract Class
abstract class Shape {
// Abstract method (must be overridden)
abstract double calculateArea();
// Concrete method (shared by all subclasses)
void display() {
System.out.println("Area: " + calculateArea());
}
}
2. Create Subclasses
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
class Square extends Shape {
double side;
Square(double side) {
this.side = side;
}
@Override
double calculateArea() {
return side * side;
}
}
3. Use the Subclasses
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
circle.display(); // Area: 78.54
Shape square = new Square(4);
square.display(); // Area: 16.0
}
}
Key Features of Abstract Classes
1. Abstract Methods
- Declared with
abstract
and no body. - Must be overridden by non-abstract subclasses.
2. Constructors
- Abstract classes can have constructors (called when subclass is instantiated).
abstract class Vehicle {
String type;
Vehicle(String type) {
this.type = type;
}
}
class Car extends Vehicle {
Car() {
super("Car"); // Calls Vehicle constructor
}
}
3. Mix Abstract and Concrete Methods
- Share reusable logic while enforcing subclass-specific behavior.
Abstract Class vs. Interface
Abstract Class | Interface |
---|---|
Can have abstract + concrete methods. | Until Java 7: only abstract methods. |
Supports instance fields. | Fields are public static final . |
Single inheritance. | Multiple inheritance. |
Use for “is-a” relationships. | Use for “can-do” capabilities. |
Java 8+: Interfaces can have default
and static
methods, but no instance fields.
Common Mistakes (and Fixes)
1. Instantiating an Abstract Class
Error:
Animal animal = new Animal(); // Error: Animal is abstract
Fix: Instantiate a subclass instead.
2. Forgetting to Implement Abstract Methods
Error:
class Circle extends Shape { } // Error: Missing calculateArea()
Fix: Add @Override double calculateArea() { ... }
to the subclass.
3. Missing abstract
Keyword
Error:
class Shape {
void calculateArea(); // Error: Abstract method must be declared abstract
}
Fix: Add abstract
to the method and class.
Best Practices
- Use Abstract Classes for Shared Logic: Avoid code duplication in related classes.
- Limit Hierarchy Depth: Prefer shallow inheritance trees.
- Combine with Interfaces: Use interfaces for additional behaviors (e.g.,
Serializable
). - Document Abstract Methods: Clarify expectations for subclasses.
Conclusion
Abstract class in Java strike a balance between code reuse and flexibility. By defining partial implementations and enforcing method contracts, they empower you to build scalable, maintainable hierarchies.
FAQs
Can an abstract class have no abstract methods?
Yes! But it must still be declared abstract
and cannot be instantiated.
Can abstract classes have final
methods?
Yes. final
methods cannot be overridden, even if they’re concrete.
Can abstract classes implement interfaces?
Yes. They can implement interfaces and defer method implementation to subclasses.