Lesson 1 of 4
The four pillars of OOP
3 min read
Object-oriented programming organises code around objects that bundle data (fields) and behaviour (methods). A class is the blueprint; an object is an instance of it.
1. Encapsulation
Hide internal state and expose behaviour through methods, so invalid states can't be created from outside.
public class BankAccount {
private double balance; // hidden
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
balance += amount;
}
public double getBalance() { return balance; } // read-only access
}Benefit: the class controls its own rules; you can change the internals without breaking callers.
2. Abstraction
Show what an object does, hide how. Users of PaymentGateway.pay() don't care whether it calls UPI or a card network.
public interface PaymentGateway {
PaymentResult pay(Order order);
}Tools: interfaces and abstract classes.
3. Inheritance
A class reuses and extends another (is-a relationship).
public abstract class Employee {
protected final String name;
protected Employee(String name) { this.name = name; }
public abstract double monthlyPay();
}
public class FullTime extends Employee {
private final double salary;
public FullTime(String name, double salary) { super(name); this.salary = salary; }
@Override public double monthlyPay() { return salary / 12; }
}
public class Intern extends Employee {
private final double stipend;
public Intern(String name, double stipend) { super(name); this.stipend = stipend; }
@Override public double monthlyPay() { return stipend; }
}Java supports single inheritance of classes but a class can implement many interfaces. Prefer composition over inheritance (has-a) when the relationship isn't truly "is-a"; deep inheritance trees are fragile.
4. Polymorphism
One interface, many forms.
- Compile-time (overloading): same method name, different parameter lists.
- Runtime (overriding): a subclass provides its own version; the JVM picks the method based on the actual object type (dynamic dispatch).
List<Employee> team = List.of(new FullTime("Asha", 600000), new Intern("Ravi", 15000));
double payroll = 0;
for (Employee e : team) payroll += e.monthlyPay(); // each object answers in its own wayAbstract class vs interface
| Abstract class | Interface | |
|---|---|---|
| Methods | abstract and concrete | abstract; default and static methods since Java 8 |
| Fields | any | only public static final constants |
| Constructors | yes | no |
| Inheritance | extend one | implement many |
| Use when | sharing code and state among closely related classes | defining a capability many unrelated classes can have (Comparable, Runnable) |
Other keywords interviewers check
static: belongs to the class, not an instance.final: variable can't be re-assigned; method can't be overridden; class can't be extended (Stringis final).this(current object) andsuper(parent class).- Access modifiers:
private< default (package) <protected<public. - Constructor chaining, and that constructors aren't inherited.
Check yourself
- Explain encapsulation with an example from a real app.
- Overloading vs overriding: which is decided at compile time?
- When would you choose an interface over an abstract class?
Sign up free to save your progress.