What are the four pillars of object-oriented programming?
The four pillars of object-oriented programming -- encapsulation, abstraction, inheritance, and polymorphism -- describe the core mechanisms by which OOP manages complexity in large codebases.
Encapsulation is the bundling of data and the methods that operate on that data within a single unit (the class), along with restricting direct external access to the internal state. In practice this means using access modifiers (private, protected, public) to expose only a controlled interface. The benefit is that you can change internal implementation details without breaking callers. A BankAccount class exposes deposit() and withdraw() but keeps the balance field private, preventing external code from setting a negative balance directly.
Abstraction means hiding implementation complexity behind a simplified interface. Users interact with what an object does, not how it does it. An interface or abstract class defines the contract; concrete implementations provide the detail. A Shape abstract class defines area() without specifying the formula -- callers do not need to know whether the shape is a circle or rectangle.
Inheritance allows a class to derive from another class, reusing and extending its behavior. A subclass inherits fields and methods from a superclass and can override or extend them. Dog and Cat classes can inherit from Animal, reusing the name field and eat() method while providing their own sound() implementations. Inheritance models is-a relationships and reduces duplication, but deep hierarchies can become brittle -- prefer composition when in doubt.
Polymorphism (from Greek: many forms) allows objects of different types to be treated uniformly through a common interface. There are two main forms: subtype polymorphism (runtime dispatch -- a variable of type Animal can hold a Dog or Cat; calling sound() invokes the correct override) and parametric polymorphism (generics/templates -- code written once works for any type). Polymorphism decouples callers from concrete types, making systems extensible without modifying existing code.
These four pillars work together: encapsulation protects internal state, abstraction hides complexity, inheritance reuses behavior, and polymorphism enables flexible behavior dispatch. Together they enable code that is modular, testable, and open for extension without requiring modification of existing logic.
Candidate can name and roughly define all four pillars and gives a basic example for each, but conflates abstraction with encapsulation or cannot explain how they differ in practice.
Candidate defines each pillar precisely, distinguishes abstraction from encapsulation clearly, explains both subtype and parametric polymorphism, gives concrete code-level examples for each, and mentions trade-offs such as when to prefer composition over inheritance.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment