Medium👤 3-5 years👤 8-15 years 1 min read

How does inheritance work in Python, and what does super() do?

Asked inAmazonMicrosoftDeloitteAccenture
#inheritance#super#mro#oop#subclass
Report issue

⚡ Short Answer

A subclass inherits attributes/methods from a parent and can add or override them. `super()` calls the parent's version of a method — most often in `__init__` to initialize the inherited part before adding subclass-specific state. Python resolves method lookup via the MRO (Method Resolution Order).

Coffee Chat Question

Concept Made Simple

How does inheritance work in Python, and what does super() do?

🧠Mind Map Answer

Remember It Faster

Subclassclass Dog(Animal)
Overrideredefine a parent method
super()call the parent's method
MROlookup order (C3 linearization)

⌨️Hands-on Keyboard

Learn by Doing

python
class Animal:
    def __init__(self, name):
        self.name = name
    def speak(self):
        return "..."

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)   # init parent part
        self.breed = breed
    def speak(self):
        return "Woof"

d = Dog("Rex", "Lab")
print(d.name, d.speak())
Output
Rex Woof

🔥What If?

Think Beyond the Expected

Why call super().__init__() instead of Parent.__init__(self)?

super() follows the MRO, so in multiple-inheritance hierarchies each parent's __init__ runs exactly once in the correct order — hard-coding Parent.__init__ can skip or double-call ancestors. super() is also refactor-safe: rename the base class and it still works.

😂Real World

Framework classes (Django views, exceptions, custom errors) are extended by subclassing and calling super() to reuse base behavior while adding your own — the backbone of code reuse in OOP Python.

🎯Interviewer's Expectation

Keywords they're listening for:

subclass inherits/overridessuper() calls parentinit the inherited partMRO resolves lookupprefer super over hardcoding parent

⚠️Common Mistakes

  • Forgetting super().__init__() (uninitialized parent state)
  • Hardcoding Parent.__init__ in multi-inheritance
  • Overusing deep inheritance instead of composition

Best Practices

  • Use super() for parent initialization/methods
  • Prefer composition over deep hierarchies
  • Keep overrides consistent with the parent contract

🔁Follow-up Questions

  • 1What is the MRO and how do you view it (__mro__)?
  • 2How does Python handle multiple inheritance / diamond problem?
  • 3Composition vs inheritance — when each?

🧩Related Technologies

MROmixinsabstract base classes

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Inheritance (Python)
Interview question: "How does inheritance work in Python, and what does super() do?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions