OOP Fundamentals • Abstraction & InterfacesMedium⏱️ ~2 min
Abstraction Mechanisms: Abstract Classes vs Interfaces
Object-oriented languages provide two primary mechanisms for abstraction, each with distinct characteristics and use cases.
Abstract Classes
Can contain: Abstract methods (no implementation) AND concrete methods (with implementation)
Can have: Instance variables, constructors, state
Inheritance: Single inheritance only
Interfaces
Can contain: Method signatures only (traditionally), default methods (modern languages)
Can have: Constants only (no instance variables)
Inheritance: Multiple interface implementation
«abstract» Vehicle
# licensePlate: String
# registrationDate: Date
# registrationDate: Date
+ getAge(): int
+ calculateToll(): Money
+ calculateToll(): Money
▲
Car
+ calculateToll()
Truck
+ calculateToll()
When to use Abstract Classes: Use when subclasses share common state and behavior (IS-A relationship with shared implementation). The abstract Vehicle class provides getAge() implementation that all vehicles inherit, but forces each subclass to define its own calculateToll() logic.
When to use Interfaces: Use when defining a contract that unrelated classes can implement (CAN-DO capability). A Printable interface can be implemented by Document, Invoice, and Report classes that share no common ancestry but all need printing capability.
Interview Tip: In modern languages like Java 8+, interfaces can have default method implementations, blurring the distinction. However, interfaces still cannot have instance state, making abstract classes appropriate when shared mutable state is needed.
💡 Key Takeaways
✓Abstract classes support both abstract and concrete methods with shared state
✓Interfaces define pure contracts without implementation (though modern languages allow default methods)
✓Abstract classes enforce IS-A relationships; interfaces define CAN-DO capabilities
✓Choose abstract classes for shared implementation; interfaces for multiple unrelated capabilities
📌 Examples
1Abstract Vehicle class shares licensePlate but forces toll calculation per vehicle type
2Printable interface allows Document, Invoice, and Report to all support printing without shared ancestry