Inheritance can be done in a number of ways. Till now, we have seen examples where a single class inherits another class.
We can also create a subclass of another subclass.
For example, a class Shape has a subclass Rectangle, and the class Rectangle has a subclass Square. In this case, Square will inherit the properties of Rectangle as well as Shape.
class Shape():
# code
class Rectangle(Shape):
# code
class Square(Rectangle):
# code
We also know that a class can have more than one subclass.
For example, a class Shape has two subclasses Rectangle and Circle. Here, both Rectangle and Circle will inherit the properties of Shape.
class Shape():
# code
class Rectangle(Shape):
# code
class Circle(Shape):
# code
Let’s see an example having the above two scenarios of inheritance.
class Shape():
def m(self):
print("This is Shape")
class Rectangle(Shape):
def m1(self):
print("This is Rectangle")
class Circle(Shape):
def m2(self):
print("This is Circle")
class Square(Rectangle):
def m3(self):
print("This is Square")
r = Rectangle() # creating object r of subclass Rectangle
c = Circle() # creating object c of subclass Circle
s = Square() # creating object s of subclass Square
r.m1()
r.m()
print("****")
c.m2()
c.m()
print("****")
s.m3()
s.m1()
s.m()
In this example, Shape
has two subclasses Rectangle
and Circle
, and thus the objects of Rectangle
and Circle
can access the methods of their respective classes as well as the method of the parent class Shape
. Rectangle
further has a subclass Square
and so the object of Square
can access the method of Square
and the methods of its ancestor classes Rectangle
and Shape
.
This was pretty straightforward, right?
In Python, a class can have more than one parent class. So, it inherits the properties of all its parent classes.
For example, a class Deer is a subclass of two classes - Animal and Herbivorous. So, it will inherit the properties of both its parent classes.
class Animal():
# code
class Herbivorous():
# code
class Deer(Animal, Herbivorous):
# code
# superclass
class Animal():
def m1(self):
print("This is parent class Animal")
# superclass
class Herbivorous():
def m2(self):
print("This is parent class Herbivorous")
# subclass
class Deer(Animal, Herbivorous):
def m(self):
print("This is child class Deer")
deer = Deer()
deer.m()
deer.m1()
deer.m2()
Here, the class Deer
has two parent classes Animal
and Herbivorous
. Thus, its object deer
can access both its parent classes’ methods m1()
and m2()
.
Look at another example.
class Area():
def getArea(self, l, b):
return l * b
class Perimeter():
def getPerimeter(self, l, b):
return 2*(l + b)
class Rectangle(Area, Perimeter):
def __init__(self, l, b):
self.length = l
self.breadth = b
def area(self):
return super().getArea(self.length, self.breadth)
def perimeter(self):
return super().getPerimeter(self.length, self.breadth)
rect = Rectangle(7, 4)
print("Area:", rect.area())
print("Perimeter:", rect.perimeter())
In this example, the class Rectangle
inherits two classes Area
and Perimeter
. The class Area
has a method getArea()
which receives length and breadth and returns the area. The class Perimeter
also has a method getPerimeter()
which receives length and breadth and returns the perimeter.
When we created the object rect
of the class Rectangle
, its constructor got called and assigned the values 7 and 4 to the attributes length
and breadth
respectively for the object rect. So the length and breadth of rect
became 7 and 4 respectively.
Then the object called the method area()
of the class Rectangle
which called the method getArea(l, b)
of its parent class Area
assigning the values 7 and 4 to the parameters l
and b
respectively. This method then returned the area of the rectangle of length 7 and breadth 4.
Similarly, we returned the perimeter of the rectangle using the class Perimeter
.
Note that in super().getArea(self.length, self.breadth)
, the getArea()
method of the parent class Area
got called, and in super().getPerimeter(self.length, self.breadth)
, the getPerimeter()
method of the class Perimeter got called.
You were already aware of the concept of inheritance. In this chapter, we saw the different ways we can implement it.