OOP Fundamentals in Java
Beginner · 20 min read · Low-Level Design, Foundations
Encapsulation, abstraction, inheritance, polymorphism — and interfaces, equality and immutability.
Every design pattern and every LLD answer is built from a handful of object-oriented ideas. Interviewers probe them directly ("What's the difference between an abstract class and an interface?") and indirectly, through the quality of your models.
The four ideas in plain words
The rest of this lesson shows each one in Java, then the details interviewers like to probe.
Objects and classes
A class is a blueprint that defines state (fields) and behaviour (methods). An object is an instance with its own state. Good OO design is about deciding which object owns which data and which behaviour — keeping data and the logic that uses it together.
Pillar 1 — Encapsulation
Bundle data with the methods that operate on it, and hide the internal representation. Callers interact through a small public interface; invariants are enforced in one place.
Pillar 2 — Abstraction
Expose what an object does, hide how it does it. Clients depend on a simplified model.
Pillar 3 — Inheritance
A subclass acquires the fields and methods of its superclass and can extend or override them. It models an is-a relationship.
Pillar 4 — Polymorphism
One interface, many implementations: the same call behaves differently depending on the object's runtime type.
Interface vs abstract class
Rule of thumb: start with an interface for the contract; add an abstract base class behind it only if several implementations share substantial code (e.g., PaymentGateway interface + AbstractHttpPaymentGateway with shared retry logic).
Access modifiers
Default to the most restrictive level that works. protected fields are a form of public API to every subclass, forever.
equals and hashCode
Value-based equality matters whenever objects go into HashMaps or HashSets — which is constantly in LLD code.
Immutability
An immutable object's state can't change after construction. Benefits: inherently thread-safe, safe to share and cache, safe as map keys, easier to reason about.
Composition: objects made of objects
Most real behaviour comes from objects collaborating: an Order has LineItems, uses a PricingPolicy, and is persisted by an OrderRepository. Inheritance answers "what is it?"; composition answers "what does it have and use?" — and composition is the mechanism behind most design patterns. See Class Relationships & UML.
Interview questions
- What's the difference between abstraction and encapsulation?
- When would you choose an abstract class over an interface in Java?
- What happens if you override equals but not hashCode?
- Is overloading polymorphism? What about overriding a static method?
Further reading
Key takeaways
- Encapsulation guards state and invariants; abstraction hides implementation behind contracts.
- Inheritance models is-a and couples tightly; polymorphism replaces type-switching with dispatch.
- Interfaces define roles and variation points; abstract classes share code within a family.
- Override equals and hashCode together; use records for value types.
- Prefer immutable value objects; confine mutability to a few well-guarded entities.
Previous: The LLD Interview Framework · Next: Class Relationships & UML