OOPs Concepts in Java
🔷 What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”. These objects can contain data (fields or attributes) and code (methods or functions). Java is a pure object-oriented language (except for primitive types like int
, char
, etc.).
OOP focuses on organizing code using objects, making it modular, reusable, and easier to debug or maintain.
🔷 Four Main Pillars of OOP in Java
1. Encapsulation
-
Wrapping data and code together into a single unit (class).
-
Data is hidden using private access modifiers, and access is provided through getters/setters.
-
Ensures data security and prevents direct modification.
📘 Example:
2. Inheritance
-
Allows a class to inherit properties and methods from another class.
-
Promotes code reuse and hierarchical classification.
📘 Example:
3. Polymorphism
-
Means “many forms”. Java allows methods to behave differently based on the context.
-
Compile-time polymorphism: Method overloading
-
Runtime polymorphism: Method overriding
📘 Example:
4. Abstraction
-
Hiding internal implementation and showing only essential features.
-
Achieved using abstract classes and interfaces.
📘 Example:
🔷 Benefits of OOP
-
🔁 Reusability through inheritance
-
🧩 Modular structure
-
🔒 Security through encapsulation
-
🚀 Easier to scale and maintain
-
📚 More realistic program design