About

Abstract Classes vs Interfaces

Abstract Classes vs Interfaces

Both abstract classes and interfaces are core abstraction mechanisms in Java, but they serve distinct architectural roles.

Abstract Classes: What Something IS

An abstract class is an incomplete blueprint that cannot be instantiated directly with new.

public abstract class Shape {
    protected String color;

    public Shape(String color) {
        this.color = color;
    }

    public abstract double calculateArea();
}

Interfaces: What Something CAN DO

An interface establishes a behavioral contract or specification that any implementing class promises to satisfy.

public interface Drawable {
    void draw();
}

Summary Comparison Table

DimensionAbstract ClassInterface
Multiple InheritanceNo (extends only one)Yes (implements many)
Instance StateAllowed (private int x;)No instance state
RelationshipIS-A (Dog IS-A Animal)CAN-DO (Bird CAN Fly)
Exercise 1

Why can an interface represent “CAN-DO” behavior across completely unrelated hierarchies?

Show solution ↓
Solution

Because interfaces define pure capabilities (Comparable, AutoCloseable, Flyable) without assuming common ancestry, allowing disparate classes (Plane, Duck) to share the same polymorphic contract.