What is inheritance?
Inheritance is the concept that allows us to create new classes based upon existing classes. The new class is called the child class and the existing class is called the ‘parent class.’
When you do this, the child class inherits or is made with the same attributes and methods of the parent class it’s based on.
This concept is especially useful when you want to create very similar classes.
The code that they have in common can be written in the parent class while the code that is different can be specifically modified in the child classes. This saves us from having to duplicate too much code.
Let’s consider an example dealing with shapes. Suppose you wanted to make classes for the two different shapes: circles and rectangles. You might do something like this to make both classes:
import math
class Rectangle:
def __init__(self, color, width, length):
self.color = color
self.width = width
self.length = length
def get_color(self):
return self.color
def set_color(self, color):
self.color = color
def get_area():
return self.width * self.length
class Circle:
def __init__(self, color, filled, radius):
self.color = color
self.radius = radius
def get_color(self):
return self.color
def set_color(self, color):
self.color = color
def get_area(self):
return math.pi * self.radius ** 2
Here, there is a lot of duplication going on. The attribute color is shared by both classes.
Moreover, the methods, get_color() and set_color() are the exact same for both classes. We can reduce this duplication by using inheritance and creating a parent class.
import math
class Shape:
def __init__(self):
self.color = 'black'
def get_color(self):
return self.color
def set_color(self, color):
self.color = color
class Rectangle(Shape): # Inherits Shape class
def __init__(self, length, width):
super().__init__() # Access parent class attributes
self.length = length # Add Child Class attributes
self.width = width
def get_area(self): # Add child class method
return self.length * self.breadth
class Circle(Shape): # Inherits Shape class
def __init__(self, radius):
super().__init__() # Access parent class attributes
self.radius = radius # Add Child Class attributes
def get_area(self): # Add child class method
return math.pi * self.radius ** 2
circle1 = Circle(5)
print(circle1.get_area())
circle1.set_color("red")
print(circle1.get_color())
OUTPUT
78.53981633974483
red
Here, we made a parent class Shape and derived two very similar child classes from it: Circle and Rectangle. The only differences were that the rectangle needs a length and width while the circle needs a radius, and the get_area methods are different.
To reduce duplication, all common elements are put in the parent Shape class. The color is set to black, and the get_color() and set_color() methods are put in place.
Then, we move on to the child classes. These classes inherit the Shape class.
class Child_class_name(Parent_class_name):
#code specific to child class
The Rectangle and Circle classes follow this syntax to make child classes that take on the color attribute and methods of the parent Shape class. Then, both child classes add their own attributes using the init() method.
To access the methods of the parent class, we use the super() method. So, to access the __init__() method of the parent class and get its attributes, we use super().__init__().
The parent class methods are already available to the child class. When we are using the child class, we can call these methods at any time. Then, we also add out own methods that are specific to the child class: the get_area() method.
Now, let’s consider the instances we made. We create the object circle1 from the child class Circle with a radius of 5. Then, we use the typical child class method get_area() to func the area of the circle.
However, after that, we use the parent class method of set_color() to set the color attribute of circle1. Then, we print out the color attribute using get_color(). As the result is red, there is proof that the parent class’s methods passed over to child class.