This article is for all you beginners out there. We will be explaining the types and uses of Class Inheritance. Before we start, try to think of Class Inheritance as a Family Tree. The concept of Class Inheritance is simple, but first let us explain: What is a Class?
A Python Class is like a Blueprint used for creating new objects. It determines the Initial variables and methods that all objects created from the class should be able to use(Inherit).
Lets create a Blueprint(a “Class”) for a car. Every car should have 4 wheels, 5 seats, windows and an engine. Now everytime we want to create a new car(a “object”) from this blueprint(“Class”), we inherit all of the initial values. But, we can change the values of these variables for each new car. Such as, we can create a sports car, where we change the number of seats -> 2, add a new variable called “Spoiler” or a new method called “Sportsmode” and so on.
By doing this we have only changed the variables of the new Object and not the entire Class. Therefore, if we wanted to create a new car from our blueprint, let's say a truck, the seats will still have a default of 5 and not the updated seats used for our sports car. This lets us re-use our Class as often as we want and we can change the variables to create unique Objects from our Class.
But what if we wanted to create a class that has the initial variables and methods of another class? This is Class Inheritance. There are 4 types of Class Inheritance:
Single Inheritance is when a new class inherits properties from one single other class. Hypothetically, this would be like a child with only one parent. The child would inherit all the properties of the one parent, and can do whatever the parent can.
Multiple Inheritance is when a new class inherits properties from more than one other class. Using the same logic as before, this would be similar to a child inheriting all properties, skills and methods from both their parents combined. Therefore this new child(“Class”) would be a combination of the two and will be allowed to do anything that the two parent classes are capable of doing.
Multilevel Inheritance is when a newly created class inherits properties through “Generations” of classes. Think back to that family tree. Initially there will be a Grandparent(The Base Class), the grandparent will pass over their variables and methods to the parent(The Intermediary Class) which can be modified and updated. Then when we create the new child Class(The Derived Class), which will inherit all the properties of the parent and the grandparent, and therefore the Derived Class will be able to use all the properties of both previously created Classes.
When we first created our Car Blueprint in paragraph 3, we created two objects of the same class. The Sport Car and the Truck. This is similar to Hierarchical Inheritance. The only difference is that instead of creating 2 car objects, we create 2 new classes which both inherit the same properties of the parent class(The Car Class). The use of this is that we can now create sports cars more efficiently as we have our own updated and uniquely modified class for creating sportscars and similarly for Trucks. Both these new classes have the same initial variables but are both uniquely modified to specialize in their use.