Default Methods and Abstract Classes
You'll learn: How default methods in interfaces have blurred the traditional boundaries between interfaces and abstract classes, and when each is still the better choice.
The Traditional Divide
Historically, the difference was clear: interfaces defined what a class could do (pure contracts), while abstract classes provided both contracts and reusable implementation. But with default methods (introduced in Java 8), interfaces can now provide method implementations too.
This overlap raises an important question: if both can have concrete methods, when should you use each?
Key Differences That Remain
Despite default methods, abstract classes still offer capabilities interfaces cannot:
- State (fields): Abstract classes can have instance variables with any access modifier. Interfaces can only have
public static finalconstants. - Constructors: Abstract classes have constructors to initialize state. Interfaces have none.
- Access control: Abstract class methods can be
protectedor package-private. Interface methods are alwayspublic. - Single vs multiple inheritance: A class extends only one abstract class but can implement many interfaces.
When to Choose What
Use an abstract class when:
- You need to maintain state across methods
- Subclasses share common fields
- You want protected members for subclass access
- You're modeling an "is-a" relationship with shared implementation
Use an interface (with default methods) when:
- You're defining capabilities multiple unrelated classes might share
- You need multiple inheritance of behavior
- You want to add methods to existing types without breaking them
- State isn't required for the default behavior
Think of it this way: abstract classes are about family resemblance (shared ancestry), while interfaces are about shared abilities (things different types can all do).
Key Takeaway:
Default methods let interfaces provide behavior, but abstract classes remain the better choice when you need state, constructors, or a true inheritance hierarchy—use interfaces for capabilities, abstract classes for shared identity.