with Real-World Examples
🔰 Introduction to OOPs in Java
Object-Oriented Programming (OOP) is a programming approach that structures a
program by bundling related properties and behaviors into individual objects. Java,
being an object-oriented language, supports four key OOP principles which help
developers create modular, reusable, and scalable applications.
Core OOP Concepts in Java:
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
🔐 Encapsulation
Encapsulation is the principle of limiting access to inner class fields but exposing them
through methods. It limits the direct modification of an object's internal state and
enhances code maintainability.
Real-World Analogy: A Cell phone is an excellent example. Its internal parts are
hidden, but users manage it through buttons and the display.
💻 Java Example:
, Summary:
• Keep variables private.
• Provide public getters and setters for controlled access.
📜 Inheritance
Inheritance allows a child class to inherit fields and methods of a parent class using the
“extends” keyword. It provides reusability and establishes a parent-child relationship.
Real-World Analogy: A 'Car' is a subclass of 'Vehicle' – it inherits characteristics of
the vehicle.
💻 Java Example: