OOP Concepts in Java (4 Pillars)
In this lesson: Encapsulation, inheritance, polymorphism and abstraction in Java explained simply with real-life examples and code.
What is OOP?
Object-Oriented Programming (OOP) is a way of writing programs by organizing code around objects instead of just functions and logic. An object bundles together data (fields) and actions (methods) that work on that data — just like a real-world object such as a car has properties (color, speed) and actions (start, brake).
Java is a fully object-oriented language — almost everything in Java lives inside a class. OOP makes large programs easier to understand, reuse and maintain, which is why it is the foundation of Android, enterprise and banking software.
Think of a class as a blueprint (like a house design) and an object as the actual house built from it. One blueprint → many houses. One class → many objects.
The Four Pillars of OOP
Every OOP concept is built on four core principles, often called the four pillars:
| Pillar | Meaning | Simple Example |
|---|---|---|
| Encapsulation | Bundle data + methods together and hide internal details using private fields with getters/setters. | ATM hides your balance; you use buttons, not the internal wiring. |
| Inheritance | One class reuses the fields and methods of another using extends. | A Car and Bike both inherit from Vehicle. |
| Polymorphism | One method name behaves differently for different objects (overloading & overriding). | draw() works for Circle, Square, Triangle. |
| Abstraction | Show only the essential features, hide the complex implementation using abstract classes/interfaces. | You drive a car without knowing how the engine works. |
A Complete Example
This small program shows a class, an object, encapsulation (private field + methods) and inheritance together:
class Animal { private String name; // encapsulation: private field Animal(String name) { this.name = name; } void eat() { System.out.println(name + " is eating"); } } class Dog extends Animal { // inheritance Dog(String n) { super(n); } void bark() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Dog d = new Dog("Tommy"); // object creation d.eat(); // inherited from Animal d.bark(); // Dog's own method } }
Tommy is eating Woof!
Why Use OOP?
- Reusability — write code once (a class) and reuse it via objects and inheritance.
- Maintainability — changes stay contained; fixing one class doesn't break others.
- Security — encapsulation hides sensitive data behind controlled methods.
- Real-world modelling — code mirrors real objects, so it's easier to reason about.
Students mix up class and object. Remember: a class is written once (the blueprint); objects are created many times from it at runtime using new.
Summary
- OOP organizes code around objects = data (fields) + actions (methods).
- Four pillars: Encapsulation, Inheritance, Polymorphism, Abstraction.
- A class is a blueprint; an object is a real instance created with
new. - OOP gives reusability, maintainability, security and real-world modelling.
