Python Inheritance
Inheritance in Python
Inheritance is an essential feature in Object-Oriented Programming.
Inheritance is the mechanism of basing a class upon another class.
The inheritance mechanism enables us to define a class that inherits all the methods and properties from another class and allows us to add more.
Parent (or base) class is the class being inherited from.
Child (or derived) class is the class that inherits from another class.
Python Inheritance Syntax
class ParentClass:
Body of parent class.
class ChildClass:
Body of child class.
Child class inherits characteristics from the parent class where new characteristics can be added to it.
The inheritance aims to provide the reusability of code, so the parent class has to write features, and the child class has to write only the unique features.
Example of Inheritance in Python
Let us take an example to understand inheritance in Python.
A rectangle is a closed figure with 4 sides. Let us assume that we have a class called Rectangle
defined as follows.
class Rectangle:
def __init__(self, length, height):
self._length = length
self._height = height
def calculateArea(self):
return self._length * self._height
The Rectangle
class has data attributes to store length
and height
. This class is initialized with a length
and a height
.
The calculateArea()
method returns the area of the rectangle.
A square is a rectangle whose adjacent sides are equal. So we can create a class called Square
which inherits from Rectangle
. This makes all the attributes of the Rectangle
class available to the Square
class.
To create a Square
class, we do not need to define all the Rectangle
class attributes again (code reusability).
Square
class can be defined as follows.
class Square(Rectangle):
def __init__(self, side_size):
super().__init__(side_size, side_size)
The Square
class is initialized with a side_size
, which is used to initialize both components of the base class.
Let us create a small program to test the behavior.
r = Rectangle(3, 6)
print("The area of the rectangle is:", r.calculateArea())
s = Square(3)
print("The area of the square is:", s.calculateArea())
Output
The area of the rectangle is: 18
The area of the square is: 9
As we can see above, even though we did not define the method calculateArea()
for class Square
, we could use it thanks to inheritance.
If an attribute or a method is not found in the child class itself, the search continues to the parent class. This happens recursively if the parent class is itself derived from other classes.
Method Overriding in Python
The above example shows that the __init__()
method was defined in both classes, Square
and Rectangle
. When this occurs, the method in the child class overrides the method in the parent class. So the __init__()
method in Square
is preferred over the __init__()
in Rectangle
.
When overriding a parent method, we tend to extend the definition preferably than replace it. This is done by calling the method in the parent class from the child class (calling Rectangle.__init__()
from __init__()
in Square
).
A better choice would be to use the built-in function super()
. So, super().__init__(2)
is equivalent to Rectangle.__init__(2)
and is recommended.
To check inheritances, we can use two built-in functions isinstance()
and issubclass()
.
The function isinstance()
returns True
if the object is an instance of the class or other classes derived from it.
Every class in Python inherits from the parent class object
.
>>> isinstance(s, Rectangle)
True
>>> isinstance(s, Square)
True
>>> isinstance(s, object)
True
>>> isinstance(s, int)
False
In the same way, issubclass()
is used to check for class inheritance.
>>> issubclass(Square, Rectangle)
True
>>> issubclass(Rectangle, Square)
False
>>> issubclass(bool, int)
True