Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 16

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.

💡 Real-world analogy

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:

PillarMeaningSimple Example
EncapsulationBundle data + methods together and hide internal details using private fields with getters/setters.ATM hides your balance; you use buttons, not the internal wiring.
InheritanceOne class reuses the fields and methods of another using extends.A Car and Bike both inherit from Vehicle.
PolymorphismOne method name behaves differently for different objects (overloading & overriding).draw() works for Circle, Square, Triangle.
AbstractionShow 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:

Java
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
    }
}
▶ Output
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.
⚠️ Common confusion

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.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicClasses and Objects Your progress: 16 / 61 (26%) · Lesson 17 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free