Single Inheritance in Python

Single Inheritance in Python

When it comes to Single Inheritance, there is going to be one child class and one parent class. So, we can say that in single inheritance, a child class inherits from a parent class.

We are soon going to have a look at an example, through which we can understand Single Inheritance, but before that, just have a look at a diagram, which tries to demonstrate single inheritance.

As you can see from the diagram, there is going to be one Child class, which is going to inherit from one Parent class.

Recall that previous example, which introduced us to how we can implement inheritance in our program. In that program as well, there is one Child class, which inherits from the Parent class. So, this becomes an example of Single Inheritance. So, you can consider referring to that example again, with an angle of understanding the single inheritance.

Single Inheritance in Python

Also, Let’s have another example for understanding Single Inheritance. Have a look at the below example –

As you can see, in the above program, there is a Vehicle class, and then there is another class Car, which inherits from the Vehicle class. The Car class object has access to the method from the Vehicle class, due to inheritance. Since here, there is one Child class, inheriting from one Parent class, we can say that this is a single inheritance. If you try to have a look at the output of the above program, you would see something like this –

This is the vehicle method!
This is the car method!

You can try out some more programming examples related to single inheritance so that you can get familiar with it.

Also, if you want to check that is some class or subclass of another class, you can always use a function here, which is the issubclass function. Let’s try to use this function in the above example, to determine whether the class Car is the subclass of the class Vehicle (which we already know that it is, but still Let’s try it), so, have a look at the modified version of the above program –

As you can see, in the last, we have the issubclass function, with help of which, we are checking whether or not, the Car class, is the subclass of the Vehicle class. This method returns True if the first argument is the subclass of the second argument basically, and it returns False otherwise. You can try to execute the program and observe the output. You can simply find the return value from the issubclass function, as True, since the Car class is the subclass of the Vehicle class.

So, this was much about the single inheritance, in which, there is one Parent class and one Child class.

You can explore more about single inheritance, and solve some examples related to single inheritance so that you can become familiar with the concept.