Object-Oriented Programming (OOP) is a programming paradigm that uses the concept of "objects" to design software. The core principles of OOP are designed to promote code reusability, modularity, and organization.
it has 4 principles:
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Encapsulation:
Focuses on how an object's data is accessed and modified, and it is achieved through access modifiers and getter/setter methods.
Encapsulation is the principle of bundling data (attributes) and methods (functions) that operate on the data into a single unit or class. It also involves restricting access to some of the object's components, which is known as data hiding.
It protects the internal state of an object and only allows modification through a well-defined interface (usually through public methods). This helps to reduce complexity and increase the robustness of the code by preventing external interference and misuse.
Inheritance
Inheritance is the mechanism by which one class (called the subclass or derived class) can inherit the properties and behaviors (attributes and methods) of another class (called the superclass or base class).
It promotes code reusability, as common functionality can be defined in a base class and shared across multiple derived classes. Inheritance also helps in establishing a natural hierarchical relationship between classes.
Abstraction:
Focuses on what an object does, and it is achieved through abstract classes and interfaces.
Abstraction is the concept of hiding the complex implementation details of an object and exposing only the necessary and relevant parts to the user.
It simplifies interaction with the object by focusing on what the object does rather than how it does it. This makes the code more understandable and easier to maintain.
Polymorphism
Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface.
It is often implemented through:
- method overloading (compile-time polymorphism).
- method overriding (runtime polymorphism)
It allows one interface to be used for a general class of actions, making the code more flexible and reusable. This enables objects of different types to be handled using a uniform interface.