Interview Q&A / Object-Oriented Programming

BeginnerIn-depth2 min readUpdated 30 Sept 2026

What are the four pillars of object-oriented programming?

SHORT ANSWER

Encapsulation (keep data and the code that uses it together, and hide the inside), abstraction (show only what matters), inheritance (reuse and extend a parent class) and polymorphism (one interface, many behaviours).

Explain it simply

Think of a car. Encapsulation: the engine is inside a cover; you cannot change the fuel mixture directly, you use the pedal. Abstraction: you only need the steering wheel, pedals and gear lever; you do not need to know how fuel injection works. Inheritance: an electric car is still a car, so it reuses what a car has and adds a battery. Polymorphism: pressing "accelerate" works on a petrol car and an electric car, but each does it in its own way.

Example

java
abstract class Payment {                 // abstraction: what every payment can do
    private final double amount;         // encapsulation: hidden inside
    Payment(double amount) { this.amount = amount; }
    double getAmount() { return amount; }
    abstract String pay();               // each type decides how
}

class UpiPayment extends Payment {       // inheritance
    UpiPayment(double amount) { super(amount); }
    String pay() { return "Paid Rs." + getAmount() + " by UPI"; }
}

class CardPayment extends Payment {
    CardPayment(double amount) { super(amount); }
    String pay() { return "Paid Rs." + getAmount() + " by card"; }
}

// Polymorphism: same call, different behaviour
Payment[] payments = { new UpiPayment(250), new CardPayment(999) };
for (Payment p : payments) System.out.println(p.pay());

How it works

  • Encapsulation uses access modifiers (private, public) so other code cannot put an object into an invalid state.
  • Abstraction uses abstract classes and interfaces to define what can be done without saying how.
  • Inheritance lets a child class reuse fields and methods of a parent with extends.
  • Polymorphism lets a parent-type variable hold any child, and the right method runs at runtime (method overriding). Method overloading, with the same name and different parameters, is compile-time polymorphism.

Real-world use

Payment gateways, notification senders (email, SMS, WhatsApp) and file exporters (PDF, Excel) are all classic places where one interface has many implementations.

Common mistakes

  • Using inheritance just to reuse code when composition ("has a") fits better.
  • Making every field public, which breaks encapsulation.
  • Confusing abstraction (hiding complexity) with encapsulation (hiding data).

What to say in the interview

"The four pillars are encapsulation, abstraction, inheritance and polymorphism. Encapsulation bundles data with the methods that change it and hides internal state. Abstraction exposes only the essential operations through interfaces or abstract classes. Inheritance lets a class reuse and extend another. Polymorphism lets the same call behave differently for different types, through overriding at runtime or overloading at compile time. For example, a payment interface with UPI and card implementations."

Follow-up questions

  • What is the difference between overloading and overriding?
  • When would you prefer composition over inheritance?
  • What is the difference between an abstract class and an interface?

#oop#fundamentals

Related questions

Practise more Object-Oriented Programming questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Object-Oriented Programming quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.