Problem
Explain the four pillars of Object-Oriented Programming, with real-life examples for each.
Be ready to discuss
- Encapsulation: bundling data with the methods that operate on it and restricting direct access via access modifiers — a BankAccount exposing deposit()/withdraw() while the raw balance field stays private, so invariants cannot be violated from outside.
- Abstraction: exposing essential behavior while hiding implementation — a car's steering wheel and pedals versus the engine internals; in code, an interface or abstract class that callers depend on instead of a concrete implementation.
- Encapsulation versus abstraction: a distinction interviewers probe — encapsulation hides state to protect it, abstraction hides complexity to simplify the contract.
- Inheritance: a subclass acquiring properties and behavior from a parent (Car and Truck extending Vehicle), plus the "is-a" test and why composition is often preferred over deep hierarchies.
- Polymorphism: one interface, different behavior by underlying type — runtime via method overriding and dynamic dispatch, compile-time via overloading; the payment-processor example where each subclass implements process() differently.
- Tying it together: how Liskov substitution keeps inheritance honest, and where each pillar shows up in a class design you have actually written.