What are the 4 pillars of OOP?
The four pillars of Object-Oriented Programming (OOP) are the core principles that guide the design and development of object-oriented systems. Understanding these pillars is essential for writing clean, reusable, and maintainable code, and they are commonly tested in programming interviews. Here are the four pillars of OOP:
1. Encapsulation
Encapsulation is the practice of bundling the data (attributes) and the methods (functions) that operate on that data into a single unit, called a class. It restricts direct access to some of the object's components, ensuring that the internal state is hidden from the outside world. This helps to protect the object's integrity and maintain its state in a controlled manner.
Example
class Car: def __init__(self, brand, model): self.__brand = brand # Private attribute self.__model = model # Private attribute # Public method to access private attributes def get_details(self): return f"Car Brand: {self.__brand}, Model: {self.__model}" car = Car("Toyota", "Corolla") print(car.get_details()) # Accessing encapsulated data through a public method
In this example, the __brand
and __model
attributes are encapsulated, and access to them is only allowed through the public get_details()
method.
2. Abstraction
Abstraction is the concept of hiding the complex implementation details and exposing only the essential features of an object. It allows programmers to focus on high-level operations while simplifying interactions with the object. Through abstraction, you can define clear interfaces without worrying about the underlying complexity.
Example
class Car: def start_engine(self): print("Starting the car engine...") # User interacts with the Car class at a high level car = Car() car.start_engine() # No need to know how the engine starts, just use the method
In this example, the method start_engine()
abstracts away the complexities of starting the car's engine, providing a simple interface for the user.
3. Inheritance
Inheritance allows one class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). It promotes code reuse, as the subclass can reuse the functionality of the superclass and even extend or override certain features.
Example
class Animal: def speak(self): print("Animal makes a sound") class Dog(Animal): # Dog inherits from Animal def speak(self): # Overriding the inherited method print("Dog barks") dog = Dog() dog.speak() # Output: Dog barks
In this example, the Dog
class inherits from the Animal
class and overrides the speak()
method to provide its own behavior.
4. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to be used for different data types, and the method behavior can change based on the object type.
Example
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") class Cat(Animal): def speak(self): print("Cat meows") # Polymorphism in action animals = [Dog(), Cat()] for animal in animals: animal.speak() # Calls the appropriate speak method for each object
In this example, both Dog
and Cat
are subclasses of Animal
. The speak()
method is overridden in both subclasses. When we loop through the animals
list, polymorphism ensures that the correct speak()
method is called based on the object's actual type.
Summary of the Four Pillars of OOP:
- Encapsulation: Bundling data and methods together and restricting direct access to the object's internal state.
- Abstraction: Hiding complex implementation details and exposing only the essential features.
- Inheritance: Allowing a class to inherit properties and behaviors from another class, promoting code reuse.
- Polymorphism: Enabling objects of different types to be treated as instances of a common superclass, allowing methods to work with objects of different classes in a unified way.
Recommended Courses
To deepen your understanding of these OOP concepts and ace your interviews, consider enrolling in the following courses from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the Coding Interview: Patterns for Coding Questions
- Grokking the System Design Interview
These courses will help you master the core principles of Object-Oriented Programming and prepare you for technical interviews.
GET YOUR FREE
Coding Questions Catalog