Object-Oriented Programming (OOP)
Programming Paradigms
Object-Oriented Programming is a programming paradigm that organizes software design around objects, which are instances of classes. An object bundles together data (attributes) and the methods (functions) that operate on that data into a single, self-contained unit.
OOP was created to overcome the challenges of procedural programming, especially the problems of managing shared, global state in large and complex systems. Instead of a top-down procedural flow, an OOP program is viewed as a collection of objects interacting with each other by sending messages (calling methods).
Core Idea: Model the real world by creating objects that represent entities, complete with their own properties and behaviors.
Analogy: A car. A car is an object.
- Attributes (Data): It has a color, a model, a current speed, and a fuel level.
- Methods (Behavior): It can
start()
,accelerate()
,brake()
, andrefuel()
.
You don't need to know the complex internal mechanics of the engine to drive the car. You interact with it through a simple interface (steering wheel, pedals). The car manages its own internal state (like engine temperature and oil pressure). This is the essence of OOP.
Languages: Java, C++, C#, Python, Ruby, and JavaScript (with prototypes) are all prominent OOP languages.
The Four Pillars of OOP
These are the four fundamental concepts that define OOP. A strong interview answer will be able to define and provide examples for each.
-
Encapsulation is the practice of bundling the data (attributes) and the methods that operate on that data within a single object. It also involves restricting direct access to an object's internal state. This is often called data hiding. Read more about encapsulation in LLD section.
-
Inheritance is a mechanism that allows a new class (the subclass or child class) to adopt the properties and methods of an existing class (the superclass or parent class). This promotes code reuse and establishes an "is-a" relationship. Read more about inheritance in LLD section.
-
Polymorphism (from Greek, meaning "many forms") allows objects of different classes to be treated as objects of a common superclass. It is the ability to present the same interface for differing underlying forms (data types). The most common form of polymorphism is method overriding. Read more about polymorphism in LLD section.
-
Abstraction is the concept of hiding complex implementation details and exposing only the essential features of an object. It focuses on what an object does, not how it does it. Abstraction is achieved through abstract classes and interfaces. Read more about abstraction in LLD section.
Summary for Interviews
- OOP organizes code around objects, which bundle data (attributes) and behavior (methods).
- Encapsulation: Hiding internal data and exposing functionality through public methods. Protects object state.
- Inheritance: Creating new classes from existing ones to reuse code. Establishes an "is-a" relationship.
- Polymorphism: Treating objects of different classes through a common interface. Allows for flexibility and code that adapts to the specific type at runtime ("many forms").
- Abstraction: Hiding implementation complexity and exposing only essential features through abstract classes and interfaces. Defines a clear contract.