Interfaces vs Abstract Classes: Initial Comparison
What You'll Learn
You'll understand the fundamental difference between interfaces having no state or constructors versus abstract classes that can have both.
The Core Difference: State and Construction
Now that you've learned both abstract classes and interfaces, let's clarify a key distinction that sets them apart at the structural level.
Interfaces: Pure Contracts
Interfaces define what a class can do without providing any implementation details about how the class stores data or initializes itself:
- No constructors: You cannot write a constructor inside an interface. There's no initialization logic.
- No instance fields: Interfaces cannot hold state (instance variables). Any fields you declare are automatically
public static finalconstants—shared by all, not unique to each instance.
Think of an interface as a job description: it lists the responsibilities but doesn't provide office space or onboarding procedures.
Abstract Classes: Partial Blueprints
Abstract classes can provide both contractual obligations (abstract methods) and shared infrastructure:
- Can have constructors: You write constructors to initialize the abstract class's fields when subclasses are created.
- Can have instance fields: Abstract classes can maintain state that all subclasses inherit and share access to.
Think of an abstract class as a building foundation with some rooms already built—it provides structure, storage, and setup instructions.
Why This Matters
If your design needs shared state (fields) or common initialization logic (constructors), use an abstract class. If you only need to define a contract without any implementation baggage, use an interface.
Key Takeaway:
Interfaces are pure contracts with no state or constructors; abstract classes can include both fields and constructors to provide shared infrastructure alongside abstract methods.