โ† CS Fundamentals
#5 most askedVery commonEasyOOP

The four pillars of OOP

New to OOP? Learn it from scratch first
Inheritance vs Composition โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

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.

    Try it ยท run and change it
    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 โ†’