Inheritance in Python — Python Technical Interview Questions & Answers
Inheritance in Python
Inheritance in Python
- Inheritance lets a child class reuse a parent. class Dog(Animal): Dog is-a Animal. Child gets parent methods and can override them. super() calls the parent version when the child still needs it. Python allows multiple inheritance — use carefully, MRO questions are later.
- Tiny override: Animal.speak → "...". Dog.speak → "woof". print(Dog().speak()) is woof. That IS-A + override + super() trio is the fresher answer. Don’t start with diamond diagrams.
- Inheritance in Python — output: Eating then Bark. Dog did not rewrite eat — it inherited it.
Animal
eat()
│ class Dog(Animal)
▼
Dog
eat() + bark()Technical Interview Tip: Tiny parent/child method override.
Code Examples for Inheritance in Python
Example
# Inheritance
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Bark")
d = Dog()
d.eat()
d.bark()
Inheritance in Python — output: Eating then Bark. Dog did not rewrite eat — it inherited it.
How to Explain Inheritance in Python to an Interviewer
Explain Inheritance as if you are teaching a junior — definition, then one tiny script.
Child(Parent). IS-A. Override + super().
What does the example print, and why?
Inheritance in Python — output: Eating then Bark
What mistake do freshers make with Inheritance?
Multiple inheritance exists.