Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Name the four pillars of object-oriented programming and give a one-line Java example of each.

The four pillars are encapsulation, abstraction, inheritance and polymorphism. Encapsulation keeps fields private behind methods that protect rules, such as a BankAccount that rejects negative deposits; abstraction exposes what an object does through a type like List while hiding how it stores data; inheritance lets a class such as SavingsAccount extend Account to reuse and extend it; and polymorphism means shape.area() on a Shape reference runs the subtype's own implementation at run time. They work together, and inheritance is the one most easily misused.

Why this question is asked

Almost every Java interview for early-career roles opens with the four pillars, and almost every candidate can list them. What separates a good answer is a concrete example of each and a sense of why each exists, because that shows we can use them when designing a class, not just name them. In this answer, we will recap what object-oriented programming tries to achieve, take each pillar in turn with a short Java example and its output, and finish with how they work together and why inheritance needs the most care.

1A Quick Recap of Object-Oriented Programming

Object-oriented programming organises code around objects: small units that hold some data, called state, together with the methods that work on that data. Instead of a program being a long list of steps that change shared variables, it becomes a set of objects that each look after their own state and ask each other to do things. The four pillars are the ideas that make this work well. Each one answers a different question: who may change the data, what callers need to know, how to reuse code, and how one call can do different things for different objects.

The four pillars at a glance
PillarJava exampleWhat it gives us
Encapsulationprivate balance, deposit() checks amount > 0Rules can't be bypassed
AbstractionList<String> names = new ArrayList<>()Callers ignore how it works
Inheritanceclass SavingsAccount extends AccountReuse and extend a class
Polymorphismshape.area() runs Circle's or Square'sOne call, behaviour per type

2Encapsulation: Keeping the Rules Inside the Object

Encapsulation means an object keeps its fields private and only lets the outside world change them through methods. The reason is that those methods can enforce rules, called invariants, that must always hold. A bank account balance should never change by a negative deposit, and if the field were public, any code could break that rule.

Java
public class BankAccount {
private long balanceCents; // nobody outside can touch it
public void deposit(long cents) {
if (cents <= 0) throw new IllegalArgumentException("deposit must be positive");
balanceCents += cents;
}
public long balance() { return balanceCents; }
}
BankAccount acc = new BankAccount();
acc.deposit(500);
System.out.println(acc.balance()); // 500
acc.deposit(-100); // IllegalArgumentException: deposit must be positive

Notice that encapsulation is not just making fields private and adding a getter and setter for each one. A public setBalance would make the private field pointless. The point is that the methods express meaningful operations that keep the object valid.

3Abstraction: Exposing What, Hiding How

Abstraction means callers work with what an object does and do not depend on how it does it. In Java we usually express this with an interface. The List interface promises that we can add, get by index and iterate, but it says nothing about whether the elements live in an array or in linked nodes.

Java
List<String> names = new ArrayList<>(); // could be LinkedList without changing the code below
names.add("Ana");
names.add("Raj");
System.out.println(names.get(1)); // Raj
System.out.println(names.size()); // 2

Because our code depends only on List, we could swap the implementation for performance reasons and nothing else would change. Abstraction reduces what each piece of code needs to know, which keeps changes small.

4Inheritance: Reusing and Extending a Class

Inheritance lets a class reuse another class's fields and methods and add or change behaviour. A subclass is declared with extends and can override methods of its superclass.

Java
public class Account {
protected long balanceCents;
public long monthlyFeeCents() { return 200; }
}
public class SavingsAccount extends Account {
private final double rate;
public SavingsAccount(double rate) { this.rate = rate; }
@Override public long monthlyFeeCents() { return 0; } // changes behaviour
public long interestCents() { return Math.round(balanceCents * rate / 12); } // adds behaviour
}

SavingsAccount inherits balanceCents and gets a new interest method, and it overrides the fee. Java allows a class to extend only one class, but it may implement many interfaces.

5Polymorphism: One Call, Behaviour Chosen at Run Time

Polymorphism, from the Greek for many forms, means the same method call does different things depending on the actual object. When we call area() through a Shape reference, the JVM looks at the real object at run time and runs that class's version. This is called dynamic dispatch.

Java
interface Shape { double area(); }
record Circle(double r) implements Shape { public double area() { return Math.PI * r * r; } }
record Square(double side) implements Shape { public double area() { return side * side; } }
List<Shape> shapes = List.of(new Circle(1), new Square(2));
for (Shape s : shapes) {
System.out.println(s.area()); // 3.141592653589793, then 4.0
}

The loop never checks which kind of shape it has. Adding a Triangle later needs no change to the loop, only a new class. That is the practical value of polymorphism: code that works with the general type keeps working as new types arrive.

6How the Pillars Work Together, and Where Inheritance Goes Wrong

The pillars support each other. Abstraction gives us a type such as Shape, polymorphism makes calls on that type do the right thing, encapsulation keeps each implementation's data valid, and inheritance is one way, though not the only way, to share code between related classes.

Of the four, inheritance is the one most easily misused. It ties the subclass closely to the parent's internals, so a change in the parent can break subclasses, and it is tempting to use it just to reuse code even when the subclass is not really a kind of the parent. A strong answer mentions the common advice to prefer composition, where a class holds another object and delegates to it, and to use inheritance only for genuine is-a relationships where the subclass can keep all the parent's promises.

7Follow-up questions

Is abstraction the same as encapsulation?

They are related but different. Encapsulation is about protecting an object's data and rules by hiding its fields. Abstraction is about exposing a simple view of what something does, often through an interface, so callers do not depend on details. An object can be encapsulated without implementing any interface, and an interface provides abstraction without saying anything about fields.

Are overloading and overriding both polymorphism?

Overloading, several methods with the same name and different parameters, is sometimes called compile-time polymorphism, because the compiler picks the method from the argument types. Overriding is run-time polymorphism, chosen from the actual object. When interviewers say polymorphism, they usually mean overriding and dynamic dispatch.

Why is composition often preferred to inheritance?

Composition depends only on another object's public methods, so the parts can change independently and can even be swapped at run time. Inheritance depends on the parent's internals, such as which methods call which, so changes in the parent can break subclasses. Composition also avoids Java's limit of one superclass.

8Conclusion

In this answer, we went through the four pillars with an example of each. Encapsulation kept BankAccount's balance private and rejected negative deposits, abstraction let our code depend on List rather than ArrayList, inheritance let SavingsAccount extend Account, and polymorphism made shape.area() run the right implementation at run time. We also saw that the pillars work together, and that inheritance is the one to use most carefully, often in favour of composition.

What to say in the interviewAbout 90 seconds aloud

Open with

The four pillars are encapsulation, abstraction, inheritance and polymorphism. Encapsulation means keeping fields private and changing them only through methods that enforce the object's rules. For example, a BankAccount with a private balance whose deposit method rejects negative amounts, so no code can put the account into an invalid state.

Then, if they want more

Abstraction means exposing what an object does while hiding how. When I declare a variable as List and create an ArrayList, the rest of my code doesn't care how the list stores its elements, so I could switch to a LinkedList without changing it.

Inheritance lets a subclass reuse and extend a superclass, like SavingsAccount extends Account, overriding the monthly fee and adding an interest method. Java allows only one superclass but many interfaces.

Polymorphism means the same call does different things depending on the actual object: if I loop over a list of Shape and call area, the JVM runs the Circle or Square version at run time, so adding a new shape doesn't change the loop.

I'd add that they work together: abstraction gives the type, polymorphism makes calls on it correct, encapsulation keeps each object valid. And inheritance is the easiest to misuse, because it couples the subclass to the parent's internals, so I usually prefer composition unless there's a genuine is-a relationship.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Name the four pillars of object-oriented programming and give a one-line Java example of...": https://scrivio-interview-library.netlify.app/java/oop-principles/4750799462/