Object-Oriented Programming (OOP) is based on four important principles:
1. Encapsulation
- Definition: Encapsulation is the concept of bundling data (attributes) and the methods (functions) that operate on the data into a single unit (a class);
- Purpose: It helps in hiding the internal details of how an object works, providing a clear interface for interacting with the object for the users of that object.
2. Inheritance
- Definition: Inheritance allows a new class (subclass or derived class) to inherit the characteristics and behaviors of an existing class (superclass or base class);
- Purpose: It promotes code reusability and establishes a relationship between classes, making it easier to manage and extend code.
3. Polymorphism
- Definition: Polymorphism means the ability of a single function or method to work in different ways based on the context or the types of objects it is operating on;
- Purpose: It enhances flexibility and enables code to be more generic, allowing the same function or method to be used with different types of objects.
4. Abstraction
- Definition: Abstraction involves simplifying complex systems by modeling classes based on the essential properties and behaviors, while ignoring unnecessary details;
- Purpose: It helps in managing complexity by focusing on what an object does without needing to understand the internal implementation details.