Python Inheritance
Inheritance allows a class to take the properties and methods of another class. The class that gives is called the parent class (or base class), and the class that receives is the child class (or derived class).
It helps to reuse code. You write common things once in the parent, and every child gets them for free. A child can also add new things or change what it got. Read Python Classes and Objects first if you are new to classes.
Create a child class
Write the parent class name in brackets after the child class name.
class Person:
def __init__(self, name):
self.name = name
def show(self):
print("Name:", self.name)
class Student(Person):
pass
s = Student("Robin")
s.show()
Name: Robin
The Student class is empty, but it can still use __init__() and show() of Person. The pass keyword is used when a class has no body.
The super() function
If the child has its own __init__(), it does not call the parent one automatically. Use super() to call the parent method, so you do not need to write the same code again.
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
def show(self):
print(self.name, "is studying", self.course)
s = Student("Robin", "Python")
s.show()
Robin is studying Python
Method overriding
When a child class has a method with the same name as the parent, the child method is used. This is called method overriding.
class Animal:
def speak(self):
print("Some sound")
class Dog(Animal):
def speak(self):
print("Bark")
class Cat(Animal):
pass
Dog().speak()
Cat().speak()
Bark
Some sound
Dog has its own speak(), but Cat has none, so it uses the one from Animal.
Types of inheritance
| Type | Meaning |
|---|---|
| Single | One child, one parent |
| Multilevel | A child of a child (grandparent, parent, child) |
| Multiple | One child with more than one parent |
| Hierarchical | Many children share the same parent |
| Hybrid | A mix of the above types |
Multilevel inheritance
class A:
def one(self):
print("A")
class B(A):
def two(self):
print("B")
class C(B):
def three(self):
print("C")
obj = C()
obj.one()
obj.two()
obj.three()
A
B
C
Multiple inheritance
class Father:
def skill(self):
print("Gardening")
class Mother:
def talent(self):
print("Singing")
class Child(Father, Mother):
pass
c = Child()
c.skill()
c.talent()
Gardening
Singing
The Dog and Cat example above is hierarchical inheritance, because both inherit from Animal.
Which method is used first?
With multiple inheritance Python searches classes in a fixed order, called the method resolution order (MRO). It goes left to right.
class A:
def hello(self):
print("A")
class B(A):
def hello(self):
print("B")
class C(A):
def hello(self):
print("C")
class D(B, C):
pass
D().hello()
print([cls.__name__ for cls in D.__mro__])
B
['D', 'B', 'C', 'A', 'object']
isinstance() and issubclass()
print(isinstance(s, Student))
print(isinstance(s, Person))
print(issubclass(Student, Person))
True
True
True
Here s is the object of the Student class created in the super() example. An object of a child class is also counted as an object of its parent class.
Summary
- Inheritance lets a child class reuse the code of a parent class.
- Use
class Child(Parent)to create a child class. - Use
super()to call the parent methods, mostly__init__(). - A child can override a parent method by using the same name.
- Python supports single, multilevel, multiple, hierarchical and hybrid inheritance.
- Next topic is Polymorphism.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |