Skip to main content

Command Palette

Search for a command to run...

Abstract Class vs. Interface

Updated
•3 min read•View as Markdown
Abstract Class vs. Interface

0. Introduction

Since Java 8 introduced default methods in interfaces, many developers wondered: Do we still need abstract classes? While interfaces have become more powerful, abstract classes continue to play an important role in Java’s type system. This post explains the difference and when to use each.


1. Multiple Inheritance

  • Interface: allows multiple implementations (implements many)

  • Abstract class: only single inheritance (extends one)
    👉 Use abstract class when you need to share a strict class hierarchy.


2. State (Fields)

  • Interface: only static final constants, no instance fields

  • Abstract class: can have instance fields, constructors, and access modifiers
    👉 Use abstract class when you need to manage shared state or utility fields.


3. Constructors

  • Interface: cannot have constructors

  • Abstract class: can define constructors to enforce initialization logic


4. Access Modifiers

  • Interface: methods are implicitly public (default methods too)

  • Abstract class: supports protected, private, and package-private
    👉 Useful for hiding common logic from external access.


5. Default Method Conflicts

If multiple interfaces define the same default method, you must explicitly override it.
Abstract classes avoid this problem due to single inheritance.


6. When to Use

Interface (with default methods)

  • Define behavior or capabilities (e.g., Comparable, Runnable)

  • When multiple inheritance of behavior is required

  • When no shared state or initialization is needed

Abstract Class

  • Share state (fields) and implementation

  • Enforce constructors and initialization logic

  • Control method visibility with access modifiers

  • Best for strong hierarchical relationships


7. Comparison Table

FeatureInterface (with default methods)Abstract Class
Multiple InheritanceA class can implement multiple interfacesA class can extend only one abstract class
FieldsOnly public static final constants allowedInstance fields allowed
ConstructorsNot allowedCan define constructors for initialization
Access ModifiersMethods are implicitly public (default methods too)Supports public, protected, private, and package-private
State ManagementCannot hold instance stateCan manage and share state across subclasses
Default Method ConflictsMust override if multiple interfaces provide same default methodNo conflict since single inheritance

8. Usage Summary

Use CasePrefer InterfacePrefer Abstract Class
Define behavior without state✅
Multiple inheritance of behavior✅
Common state/fields required✅
Require constructors✅
Fine-grained access control (protected, private)✅
Avoid default method conflicts✅

✨ Conclusion

Default methods in interfaces expanded their power, but abstract classes are still necessary when state, initialization, or strict hierarchy are required.

  • Use interfaces to define contracts and allow flexibility.

  • Use abstract classes when you need shared state, constructors, or controlled access.


🔗 References