PY-101 ยท Object-oriented Python
Inheritance
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Inheritance lets a new class (the child) start from an existing class (the parent) and get all its attributes and methods, written class Child(Parent):. The child can add new methods or override existing ones, and call the parent's version with super(). It avoids copying code between similar classes.
- Create a child class
- Override a method
- Call the parent with super()
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
class Employee:def __init__(self, name, salary):self.name = nameself.salary = salarydef describe(self):return f"{self.name} earns {self.salary}"class Manager(Employee):def __init__(self, name, salary, team):super().__init__(name, salary)self.team = teamdef describe(self):return super().describe() + f" and leads {len(self.team)} people"m = Manager("Asha", 90000, ["Ravi", "Li"])print(m.describe())print(isinstance(m, Employee))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Asha earns 90000 and leads 2 people
True ๐ฏ Write class Dog(Animal) that overrides sound() to return "Woof". Print Dog("Rex").speak(), which should show: Rex says Woof
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
class Dog(Animal): def sound(self): return "Woof"
- The problemA company has employees and managers. Managers are employees too, with one extra thing: a team. Copying the whole Employee class would mean fixing every bug twice.
- The Python idea
class Child(Parent):Inheritance lets a new class (the child) start from an existing class (the parent) and get all its attributes and methods, written class Child(Parent):. - In real lifeIn Django you write class Order(models.Model): and inherit saving, loading and querying without writing any of it.
Start with a question
A company has employees and managers. Managers are employees too, with one extra thing: a team. Copying the whole Employee class would mean fixing every bug twice.
Frameworks are built on inheritance: you subclass their base classes to plug in your own behaviour.
Explain it like I'm 10
A child class is like a phone's new model: it keeps everything the old model did, adds a better camera, and maybe changes one or two things.
The technical explanation
class Manager(Employee): gives Manager everything Employee has.
Defining a method with the same name overrides it. super().__init__(...) runs the parent's version first.
isinstance(m, Employee) is True for a Manager, because a Manager is an Employee.
Syntax
class Child(Parent):
def __init__(self, a, b):
super().__init__(a)
self.b = b Real-world scenario
In Django you write class Order(models.Model): and inherit saving, loading and querying without writing any of it.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Polymorphism โQuestions people ask
Does Python support multiple inheritance?
Yes: class C(A, B):. Python uses the method resolution order (C.__mro__) to decide which parent's method wins. Keep it simple and use it sparingly.
What is composition vs inheritance?
Inheritance: a class is a specialised version of another. Composition: a class holds other objects as attributes. Prefer composition when the relationship is "has a".
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Does Python support multiple inheritance?
Yes: class C(A, B):. Python uses the method resolution order (C.__mro__) to decide which parent's method wins. Keep it simple and use it sparingly.
beginner What is composition vs inheritance?
Inheritance: a class is a specialised version of another. Composition: a class holds other objects as attributes. Prefer composition when the relationship is "has a".
intermediate How do you write a child class of Animal?
class Dog(Animal):. Put the parent in brackets.
intermediate What does super() do?
Calls the parent class's version. It reaches the parent's methods.
Quick revision
Remember
- Create a child class
- Override a method
- Call the parent with super()
- Use inheritance for "is a" relationships (a Manager is an Employee). For "has a" (a Car has an Engine), store one object inside another instead.
Syntax
class Child(Parent):
def __init__(self, a, b):
super().__init__(a)
self.b = b Official documentation
- Inheritance (docs.python.org)