Four Pillars of OOP in Java
Fast recall
Quick revision
Core answer
extends, overriding and interfaces, and contracts that hide implementation details. These ideas often work together, but one line of code does not have to demonstrate all four.Whether the learner can explain each OOP idea with a correct Java mechanism and a concrete example.
Reciting slogans such as 'interface is 100% abstraction' or claiming one statement automatically uses all four pillars.
Explain the boundary of each idea and show `List<String> names = new ArrayList<>()` as abstraction and subtype polymorphism without overclaiming.
Interview-ready guide
Interview answer
The four commonly taught OOP pillars are encapsulation, inheritance, polymorphism, and abstraction. Encapsulation means an object controls access to its representation—for example, a bank account keeps its balance private and validates changes through methods. Inheritance lets a class extend one superclass when it truly is a subtype. Polymorphism lets a caller use a parent type or interface while an overridden instance method runs for the actual object. Abstraction gives callers a useful contract without requiring them to know the implementation.
For example, List shows abstraction and runtime substitutability: my code depends on the List operations, and I can choose an ArrayList implementation. I would not claim that this single line proves all four pillars. In real code the ideas are tools: I use inheritance only for a stable subtype relationship, keep state behind clear operations, and depend on the smallest useful abstraction.
Four Pillars of OOP in Java
Direct Answer
The four commonly taught OOP pillars are encapsulation, inheritance, polymorphism, and abstraction. Java supports them with access control and class APIs, extends, overriding and interfaces, and contracts that hide implementation details. These ideas often work together, but one line of code does not have to demonstrate all four.
Code Example
// ABSTRACTION: contract — tells callers what a Shape can do, not how
abstract class Shape {
private String color; // ENCAPSULATION: private field
Shape(String color) { this.color = color; }
public String getColor() { return color; } // controlled access
public abstract double area(); // subclasses must implement 'how'
// Overloading — compile-time polymorphism (same name, different params)
public void describe() {
System.out.println(color + " shape");
}
public void describe(boolean verbose) {
System.out.println(color + " shape, area=" + area());
}
}
// INHERITANCE: Circle IS-A Shape
class Circle extends Shape {
private final double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double area() { // POLYMORPHISM: runtime override
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private final double width, height;
Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double area() { return width * height; }
}
public class OopDemo {
public static void main(String[] args) {
Shape s1 = new Circle("red", 5); // parent reference, child object
Shape s2 = new Rectangle("blue", 4, 6);
// Runtime dispatch — JVM picks Circle.area() or Rectangle.area()
System.out.println(s1.area()); // 78.54
System.out.println(s2.area()); // 24.0
s1.describe(); // overloaded method — no verbose output
s1.describe(true); // compile-time dispatch picks describe(boolean)
}
}
Common Mistakes & Pitfalls
- Treating each pillar as one keyword: a useful abstraction or encapsulated design comes from the API boundary, not merely writing
interfaceorprivate. - Calling every interface '100% abstract': interfaces may contain default, static, and private methods.
- Confusing overloading and overriding: overload selection is compile-time; overridden instance methods use runtime dispatch.
- Claiming one declaration proves all four:
List<String> names = new ArrayList<>()directly shows abstraction and subtype polymorphism, not every design property.
Check your understanding
Answer these without looking back at the article.
- Which OOP pillar is most important in your opinion and why?
- How do the four pillars work together in a real application?
- What are SOLID principles and how do they relate to OOP pillars?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.