OOPs Concepts in Java: Encapsulation, Abstraction, Inheritance, Polymorphism

Object-Oriented Programming (OOPs) in Java is built around four core concepts that work together to make code modular, reusable, and easier to maintain.

1. Encapsulation

Encapsulation bundles data and methods into a single class and restricts direct access to fields using private access modifiers and public getters/setters.

class Account {
    private double balance;
    public double getBalance() { return balance; }
}

2. Abstraction

Abstraction hides implementation details and exposes only the essential features, typically using abstract classes or interfaces.

abstract class Shape {
    abstract double area();
}

3. Inheritance

Inheritance allows a class to acquire the fields and methods of another class using the extends keyword, promoting code reuse.

class Vehicle {
    void start() { System.out.println("Vehicle starting"); }
}
class Car extends Vehicle { }

4. Polymorphism

Polymorphism allows one interface or method to take multiple forms — through method overloading (compile-time) and method overriding (runtime).

class Shape {
    void draw() { System.out.println("Drawing a shape"); }
}
class Circle extends Shape {
    @Override
    void draw() { System.out.println("Drawing a circle"); }
}

Why These Concepts Matter

  • They make large codebases easier to organize and reason about
  • They promote code reuse and reduce duplication
  • They make programs more flexible and easier to extend over time
Together, these four pillars are what make Java a true object-oriented language, and mastering them is essential before moving to design patterns and frameworks.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course

How the Four Pillars Reinforce Each Other

These concepts are not four separate tricks; they lock together. Encapsulation hides a class's data behind methods, which is exactly what abstraction needs to expose only a clean surface. Inheritance lets a child reuse that surface, and polymorphism lets code call the surface without caring which child is really behind it. Remove one pillar and the others weaken — for example, polymorphism is far less useful if fields are public and callers bypass the methods.

A Small Example That Uses All Four

abstract class Payment {            // abstraction
    private double amount;          // encapsulation
    Payment(double amount) { this.amount = amount; }
    double getAmount() { return amount; }
    abstract void pay();
}
class CardPayment extends Payment { // inheritance
    CardPayment(double a) { super(a); }
    void pay() { System.out.println("Card: " + getAmount()); }
}
class UpiPayment extends Payment {
    UpiPayment(double a) { super(a); }
    void pay() { System.out.println("UPI: " + getAmount()); }
}

Payment p = new UpiPayment(500);    // polymorphism
p.pay();                            // runs UpiPayment.pay()

The variable is typed Payment, yet at runtime Java calls the correct pay(). Adding a new payment type later needs no change to code that already loops over Payment objects.

Compile-Time vs Runtime Polymorphism

Overloading (same method name, different parameters) is resolved by the compiler before the program runs. Overriding (a child replacing a parent method) is resolved while the program runs, based on the real object. Beginners often confuse the two; the quick test is whether the decision depends on argument types (overloading) or on the actual object (overriding).

Where Beginners Go Wrong

  • Marking fields public and calling encapsulation done — encapsulation means controlled access, not just grouping data.
  • Using inheritance for code reuse when the classes are not truly a "is-a" relationship; composition often fits better.
  • Forgetting @Override, which silently turns a misspelled override into a brand-new method.
  • Deep inheritance chains that become hard to follow; two or three levels is usually plenty.

A useful habit is to name each pillar as you read code. The private fields are encapsulation. The abstract method or interface is abstraction. The extends keyword is inheritance. The overridden method called through a parent reference is polymorphism. Spotting them by sight makes unfamiliar code far quicker to understand, and it shows you where a design leans on one pillar while neglecting another.

It also helps to keep abstraction and encapsulation apart, since they are easily blurred. Encapsulation is about hiding data and guarding how it changes; abstraction is about hiding complexity and exposing only a simple idea of what something does. You can have one without the other, but strong designs usually combine both: a clean public surface backed by well-protected internal state.

Takeaway: treat the four pillars as one system. Hide data, expose intent, reuse through real "is-a" links, and let runtime pick the right behaviour — that is what makes Java code easy to extend.