OOP in Java: Encapsulation, Inheritance, Polymorphism, Abstraction
The four OOP pillars in Java are encapsulation, inheritance, polymorphism and abstraction. Interviewers want each explained with code, plus overloading vs overriding and composition over inheritance.
What you will learn
- Explain each pillar with a Java example
- Compare method overloading and overriding
- Know why Java has no multiple inheritance of classes
- Prefer composition when it fits
Key concepts
Encapsulation
Keep fields private and expose behaviour through methods that protect the rules (a balance cannot go negative).
Inheritance
A class extends another to reuse and specialise it (SavingsAccount extends Account). Java allows one superclass but many interfaces.
Polymorphism
One reference type, many behaviours. Compile-time polymorphism is overloading (same name, different parameters). Runtime polymorphism is overriding: the JVM calls the subclass method through dynamic dispatch.
Abstraction
Expose what something does and hide how: interfaces and abstract classes such as PaymentGateway with charge(), implemented by Razorpay and Stripe classes.
Step by step
Overriding rules
- Same name and parameters
- Return type same or a subtype (covariant)
- Cannot reduce visibility
- Cannot throw broader checked exceptions
- static, private and final methods cannot be overridden
- Use @Override so the compiler checks it
Examples
All four pillars in one example
public interface PaymentGateway { // abstraction
Receipt charge(Money amount);
}
public abstract class Account {
private BigDecimal balance = BigDecimal.ZERO; // encapsulation
public void withdraw(BigDecimal amt) {
if (amt.compareTo(balance) > 0) throw new IllegalStateException("Insufficient funds");
balance = balance.subtract(amt);
}
public abstract BigDecimal monthlyInterest();
}
public class SavingsAccount extends Account { // inheritance
@Override
public BigDecimal monthlyInterest() { return new BigDecimal("0.0035"); }
}
Account a = new SavingsAccount(); // polymorphism
a.monthlyInterest(); // calls SavingsAccount's versionPractise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Write an overloaded add method for int, double and three ints; explain how the compiler picks one.
- Refactor a deep inheritance tree into composition.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Why does Java not support multiple inheritance of classes?
To avoid the diamond problem: if two parents define the same method, which one does the child inherit? Java allows many interfaces instead, and if two default methods clash the class must override and choose explicitly.
Can you override a static method?
No. A static method with the same signature in a subclass hides the parent's one; which one runs depends on the reference type at compile time, not the object, so there is no runtime polymorphism.
Composition or inheritance?
Use inheritance for a true is-a relationship you control. Prefer composition (has-a, delegating to a field) for reuse, because it is looser, easier to test and lets you swap behaviour at runtime.
Summary
Encapsulation guards state, inheritance reuses and specialises, polymorphism lets one call behave differently, abstraction hides details. Know the overriding rules and prefer composition for reuse.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus