Interview question
What is Object-Oriented Programming (OOP) and what are its 4 pillars? Object-Oriented Programming (OOP) क्या है और इसके 4 pillars क्या हैं?
Answer
OOP is a programming paradigm based on objects and classes. The 4 pillars of OOP are: Encapsulation, Inheritance, Polymorphism, and Abstraction.
| Pillar | Description | Example |
|---|---|---|
| Encapsulation | Bundling data and methods, hiding internal details | Private variables with getters/setters |
| Inheritance | Derive new classes from existing ones | class Dog(Animal): pass |
| Polymorphism | Objects behave differently based on type | Method overriding, duck typing |
| Abstraction | Hide complexity, show only essential features | Abstract classes, interfaces |
# Encapsulation - hiding internal details
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
# Inheritance - reusing code
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return 'Woof!'
# Polymorphism - same method, different behavior
dog = Dog()
cat = Cat()
print(dog.speak()) # Woof!
print(cat.speak()) # Meow!
# Abstraction - hide complexity
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2OOP एक programming paradigm है जो objects और classes पर based है। 4 pillars: Encapsulation, Inheritance, Polymorphism, Abstraction
| Pillar | Description |
|---|---|
| Encapsulation | Data और methods को bundle करना, internal details छुपाना |
| Inheritance | Existing classes से नए classes derive करना |
| Polymorphism | Objects type के आधार पर अलग तरीके से behave करते हैं |
| Abstraction | Complexity छुपाना, essential features दिखाना |
Was this answer clear?