4 Pillars - What, How, and Why ?¶
Encapsulation¶
What: Hiding the internal details of an object and only exposing necessary parts through public methods.
Why: It helps in data hiding and ensures controlled access to variables.
How: Use private variables to restrict direct access and provide getters and setters to access and modify the data.
Example
Inheritance¶
What: Allows a class (child/subclass) to acquire the properties and behaviors of another class (parent/superclass).
How: Use the extends
keyword.
Why: Promotes code reusability and establishes a parent-child relationship.
Example
Polymorphism¶
What: Ability to process objects differently based on their data type or class.
- Compile-time polymorphism: Method overloading (same method name, different parameters).
- Runtime polymorphism: Method overriding (subclass redefines a method of the superclass).
Why: Increases flexibility and supports dynamic method invocation.
Example
Abstraction¶
What: Hiding the complex implementation details and only exposing the essential features.
Why: Helps in achieving modularity and loose coupling between components.
How: Use abstract classes and interfaces.
Example
Summary¶
Aspect | Encapsulation | Inheritance | Polymorphism | Abstraction |
---|---|---|---|---|
Definition | Bundling data and methods together and restricting access to data. | Mechanism for a subclass to acquire properties of a parent class. | Allowing methods to take different forms (overloading/overriding). | Hiding implementation details while showing only essential features. |
Focus | Protecting data and providing controlled access. | Code reuse and establishing a parent-child hierarchy. | Dynamic behavior based on object type. | Simplifying complex systems by exposing only key details. |
Achieved Through | Using private fields, and public getters/setters. | Using the extends keyword to derive subclasses. |
Overloading (compile-time) and overriding (runtime). | Using abstract classes or interfaces . |
Key Benefit | Data hiding and modular code. | Reduces redundancy and promotes code reuse. | Flexibility and extensibility of behavior. | Promotes loose coupling and modularity. |
Access Modifiers | Requires private , protected , or public . |
Involves all inheritance-accessible modifiers. | Leverages method visibility across class hierarchies. | Abstract methods can be protected or public (not private). |
Real-World Analogy | A capsule with medicine inside it hides the internal components. | A child inheriting traits from their parents. | A shape object behaving differently as circle/square. | A remote control exposing buttons without showing internal circuits. |
Code Dependency | Independent within the class. | Dependent on parent-child relationship. | Involves multiple forms of a single method/class. | Can work with unrelated classes sharing common behavior. |