What are the four pillars of Object-Oriented Programming (OOP)?
The four pillars of Object-Oriented Programming (OOP) are encapsulation, abstraction, inheritance, and polymorphism, and they work together to create flexible and maintainable code. Encapsulation is like a protective shield for your data, wrapping it inside a class and only allowing controlled access through methods like getters and setters. This ensures that data isn’t accidentally or improperly modified. Abstraction simplifies things by focusing on what an object does rather than how it does it. It hides unnecessary details, making your code easier to understand and work with, often achieved through abstract classes or interfaces. Inheritance is a way to reuse code by letting one class (child) inherit the properties and methods of another (parent), saving time and avoiding redundancy. Finally, polymorphism is about flexibility, allowing a single method or object to take many forms. For example, the same method can behave differently depending on the situation—either by overloading it with different parameters or by overriding it in a child class. These four principles make your code more organized, scalable, and easier to manage, helping you tackle complex programming problems more effectively.
Let’s understand them in more details.
1. Encapsulation
- Definition: Encapsulation is the practice of bundling data (fields) and methods (functions) that operate on that data into a single unit, typically a class.
- Purpose: It restricts direct access to some of an object’s components, maintaining control over data integrity.
- Implementation: Achieved through access modifiers like
private
,protected
, andpublic
. For example:public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2. Abstraction
- Definition: Abstraction focuses on exposing only essential details to the user while hiding the internal implementation.
- Purpose: Simplifies the complexity of a system and improves code maintainability.
- Implementation: Achieved using abstract classes and interfaces. For example:
abstract class Animal { abstract void makeSound(); } class Dog extends Animal { void makeSound() { System.out.println("Woof"); } }
3. Inheritance
- Definition: Inheritance allows a class (subclass) to inherit properties and behaviors (methods) from another class (superclass).
- Purpose: Promotes code reuse and establishes a parent-child relationship between classes.
- Implementation: Achieved using the
extends
keyword. For example:class Animal { void eat() { System.out.println("This animal eats food."); } } class Dog extends Animal { void bark() { System.out.println("Woof!"); } }
4. Polymorphism
- Definition: Polymorphism allows objects to be treated as instances of their parent class, enabling one interface to represent different underlying forms.
- Purpose: Enhances flexibility and extensibility in code.
- Types:
- Compile-time Polymorphism (Method Overloading): Achieved by defining multiple methods with the same name but different parameters.
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
- Runtime Polymorphism (Method Overriding): Achieved by redefining a method in the subclass.
class Animal { void makeSound() { System.out.println("Some generic animal sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Woof"); } }
- Compile-time Polymorphism (Method Overloading): Achieved by defining multiple methods with the same name but different parameters.
These four pillars are fundamental to designing robust and maintainable object-oriented systems.
Check out the video for a detailed explanation: