The Four Pillars of OOP, Explained With One Running Java Example
"Name the four pillars of object-oriented programming" is a guaranteed question in BCA, BSc and engineering vivas. Reciting four definitions gets you half the marks. Showing each pillar solving a real problem gets you the rest. We'll use one example throughout: a bank account.
The problem OOP solves
Without objects, you have loose variables and functions: balance, deposit(), withdraw(), scattered everywhere. Anyone can set balance = -5000 directly, and nothing stops them. OOP bundles the data and the operations that are allowed on it into one unit, the class.
1. Encapsulation: protect the data
Encapsulation means keeping fields private and exposing controlled methods.
class Account {
private double balance; // nobody outside can touch this directly
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false; // refuses overdraw
}
public double getBalance() { return balance; }
}
Because balance is private, the only way to change it is through deposit and withdraw, and both enforce the rules. The invariant "balance is never negative" is guaranteed by the class itself.
Viva line: "Encapsulation hides internal state and forces access through methods, so the class can protect its own rules."
2. Inheritance: reuse and specialise
A SavingsAccount is an Account with extra behaviour. Instead of copying code, it extends:
class SavingsAccount extends Account {
private double rate = 0.04;
public void addInterest() {
deposit(getBalance() * rate); // reuses Account's deposit
}
}
SavingsAccount automatically has deposit, withdraw and getBalance. It adds only what is new. This models an "is-a" relationship: a savings account is an account.
Viva line: "Inheritance lets a child class reuse and extend a parent class, modelling an is-a relationship and avoiding duplicated code."
Note: use inheritance only when the is-a relationship is real. If you only need to use another class's features, prefer composition ("has-a").
3. Polymorphism: one call, many behaviours
Suppose current accounts allow an overdraft but savings accounts do not. Both override withdraw:
class CurrentAccount extends Account {
private double overdraftLimit = 5000;
@Override
public boolean withdraw(double amount) {
if (amount > 0 && amount <= getBalance() + overdraftLimit) {
// allowed to go negative up to the limit
return true;
}
return false;
}
}
Now the same line of code behaves differently depending on the real object:
Account a = new CurrentAccount(); // reference of the parent type
a.withdraw(3000); // runs CurrentAccount's version
The reference type is Account, but Java runs the method of the actual object at runtime. That is runtime polymorphism (dynamic method dispatch). It is why a bank's code can loop over a list of mixed accounts and call withdraw on each without knowing their exact types.
There is also compile-time polymorphism: method overloading, where several methods share a name but differ in parameters (deposit(double) and deposit(double, String note)).
Viva line: "Polymorphism means the same method call can behave differently depending on the object. It is achieved by overriding at runtime and overloading at compile time."
4. Abstraction: show only what matters
Abstraction means exposing what something does and hiding how. In Java you do this with abstract classes and interfaces:
interface Payable {
boolean pay(double amount); // what, not how
}
Code that needs to pay something depends only on Payable. It does not care whether the implementation is a card, a wallet or an account, and you can swap implementations without changing the caller.
A driver uses the steering wheel and pedals (the interface) without knowing the engine internals (the implementation). That is abstraction.
Encapsulation vs abstraction (the question that follows)
| Encapsulation | Abstraction | |
|---|---|---|
| Concerned with | Protecting the data | Hiding the complexity |
| Achieved by | Private fields and public methods | Abstract classes and interfaces |
| Question it answers | "Who may change this?" | "What do I need to know to use this?" |
They work together, but they solve different problems.
Summary
| Pillar | One-line purpose | Keyword |
|---|---|---|
| Encapsulation | Protect the data | private + getters/setters |
| Inheritance | Reuse and specialise | extends |
| Polymorphism | One interface, many forms | Overriding / overloading |
| Abstraction | Hide the "how" | abstract, interface |
Sumantrak™ is an AI mentor built for Indian engineering, diploma, BCA and BSc CS/IT students — Java, C++, Python, and the rest of your syllabus, explained like this. Free to start →