Name the four pillars of object-oriented programming and give a one-line Java example of each.
Show the explanation
- Encapsulation — keep data private and expose behaviour through methods, so the object controls its own state:
private double balance;withdeposit()andgetBalance(). - Abstraction — show what an object does and hide how:
interface Payment { void pay(double amount); }. - Inheritance — a class reuses and extends another:
class SavingsAccount extends Account. - Polymorphism — one call, behaviour chosen by the actual object:
Payment p = new UpiPayment(); p.pay(100);runs the UPI version.
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()); // polymorphismA 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.
