#5 most askedVery commonEasyOOP
The four pillars of OOP
New to OOP? Learn it from scratch firstInheritance vs Composition โ
๐ข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.Explain encapsulation, abstraction, inheritance and polymorphism with a short example.
๐ค Answer it out loud, then check yourself
Say your answer as if the interviewer is listening (30โ90 seconds). Then reveal what a strong answer covers and tick what you said.
class Shape:
def area(self):
raise NotImplementedError
class Circle(Shape):
def __init__(self, r):
self._r = r # encapsulated state
def area(self):
return round(3.14159 * self._r ** 2, 1)
class Square(Shape):
def __init__(self, side):
self._side = side
def area(self):
return self._side ** 2
for s in [Circle(1), Square(3)]: # polymorphism: same call, different behaviour
print(type(s).__name__, s.area())
Answer out loud first, then compare.
Want to rehearse it?Answer this to the AI interviewer and get a verdict โ