Skip to content
UniCircle
EasyConceptualOOP concepts

The four pillars of OOP in Java

Question JAVA-OOP-009

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

Show the explanation
  1. Encapsulation — keep data private and expose behaviour through methods, so the object controls its own state: private double balance; with deposit() and getBalance().
  2. Abstraction — show what an object does and hide how: interface Payment { void pay(double amount); }.
  3. Inheritance — a class reuses and extends another: class SavingsAccount extends Account.
  4. Polymorphism — one call, behaviour chosen by the actual object: Payment p = new UpiPayment(); p.pay(100); runs the UPI version.
Java
interface Shape { double area(); }                       // abstraction
record Circle(double r) implements Shape {               // inheritance (of a type)
    public double area() { return Math.PI * r * r; }     // encapsulated state: r
}
Shape s = new Circle(2);
System.out.println(s.area());                            // polymorphism

A good follow-up point: modern advice is to prefer composition over inheritance — reuse code by holding objects rather than extending classes, unless there is a true "is-a" relationship.

Original UniCircle question. Last reviewed 22 Oct 2026.