1What are the four pillars of OOP? Explain each with an example.
- Encapsulation: keep data private and expose controlled methods. A
BankAccounthas a privatebalanceand adeposit()method that rejects negative amounts. - Abstraction: expose what an object does, hide how. A
PaymentGatewayinterface withpay(order); the caller doesn't know whether it uses UPI or cards. - Inheritance: a class reuses and extends another (
Intern extends Employee), modelling an "is-a" relationship. - Polymorphism: the same call behaves differently depending on the object.
employee.monthlyPay()returns salary/12 for aFullTimeemployee and the stipend for anIntern. Compile-time polymorphism is overloading; runtime polymorphism is overriding with dynamic dispatch.
Tie each to a benefit: encapsulation protects invariants, abstraction reduces coupling, inheritance reuses code, polymorphism lets you add new types without changing callers.