What are the four pillars of object-oriented programming?
EasyConceptOOP & SOLID
Answer
The four pillars are encapsulation, abstraction, inheritance and polymorphism. Together they let you model a problem as objects that hide their internals and can be extended safely.
- Encapsulation: bundle data with the methods that operate on it and restrict direct access, for example private fields with validated setters.
- Abstraction: expose what an object does rather than how, through interfaces or abstract classes, such as a
PaymentMethodwith apay()method. - Inheritance: one class reuses and extends another (a
Caris aVehicle), modelling an is-a relationship. - Polymorphism: one interface, many implementations. Compile-time polymorphism is method overloading; run-time polymorphism is method overriding, resolved through dynamic dispatch.
More in OOP & SOLID
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What are the four pillars of object-oriented programming? | Concept | Easy | Read the answer to What are the four pillars of object-oriented programming? | |||
| 2 | What is the difference between an abstract class and an interface? | Concept | Easy | Read the answer to What is the difference between an abstract class and an interface? | |||
| 3 | What are the SOLID principles? | Concept | Medium | Read the answer to What are the SOLID principles? | |||
| 4 | Why is composition often preferred over inheritance? | Concept | Medium | Read the answer to Why is composition often preferred over inheritance? |