OOP and SOLID · core
The four pillars with a real example
Encapsulation protects the rules of an object, abstraction hides how behind a simple interface, inheritance shares behaviour carefully, and polymorphism runs different code behind the same call. Show them with one real example, not Animal and Dog.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Use one running example such as payments: a PaymentMethod interface with pay(amount), and UpiPayment, CardPayment and NetBankingPayment classes.
- Encapsulation: a Wallet keeps its balance private and changes it only through debit and credit, which check the rules (no negative balance). It protects rules, not only hides fields.
- Abstraction: checkout calls paymentMethod.pay(amount) and does not know UPI handles or card networks. The interface shows what, not how.
- Inheritance: shared behaviour sits in a base class (for example a base processor that logs and records every payment). Use it carefully. Prefer interfaces plus composition for variation.
- Polymorphism: the same pay() call runs different code for each type at run time (overriding, dynamic dispatch). Overloading is different: it is chosen at compile time.
- A getter and a setter for every field break encapsulation, because the object no longer guards its own rules.
- Abstraction and encapsulation are different ideas: abstraction is about the outside view, encapsulation is about protecting the inside state.
The red pen
Common traps: mistake → fix
Mistake: Encapsulation means private fields
Fix: private fields with a setter for everything protect nothing; the object must enforce its own rules.
Mistake: Abstraction and encapsulation are the same
Fix: one hides complexity from the caller, the other guards state.
Mistake: Inheritance is the main way to reuse code
Fix: composition is often better; inheritance is for a true, stable is-a.
Easy to hard
Questions interviewers ask, easy to hard
- Explain the four pillars of OOP.
- Give a real example of polymorphism from a payment system.
- What is the difference between overloading and overriding?
- Why are getters and setters on every field a problem?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.