Python Single Inheritance
Inheritance means to receive something from one’s parents or ancestors. The concept of inheritance is very similar in cases of classes where a class inherits all the properties and methods of its previous class that it is inheriting from.
“Inheritance is the ability to define a new class(child class) that is a modified version of an existing class(parent class)”
The above syntax consists of two classes declared. One is the parent class or by other means, the base class, and the other one is the child class which acts as the derived class.
Two common terms related to inheritance are as follows:
Parent: The parent class is the one that is giving access to its methods or properties to the child class or derived class.
Child: Child class is the one that is inheriting methods and properties from the parent class.
The class that is inheriting, i.e., the child class, can inherit all the functionality of the parent class and add its functionalities also. As we have already discussed that each class can have its constructors and methods, so in case of inheritance the child class can make and use its constructor and also can use the constructor of the parent class. We can simply construct it as we did for the parent class but OOP has provided us with a simple and more useful solution known as Super().
We will be discussing super() and overriding in our Super() and Overriding In Classes tutorial of the course.
Single inheritance exists when a class is only derived from a single base class. Or in other words when a child class is using the methods and properties of only a single parent class then single inheritance exists. Single inheritance and Multiple inheritance are very similar concepts, the only major difference is the number of classes.
Different forms of Inheritance:
Single inheritance: When a child class inherits from only one parent class then it is called single inheritance.
Multiple inheritance: When a child class inherits from multiple parent classes then it is called multiple inheritance.
Below is an example of single inheritance in Python.
Advantages of Inheritance:
It increases the code’s reusability as we do not have to copy the same code again to our new class.
It makes the program more efficient.
We can add more features to our already built class without modifying it or changing its functionality.
It provides a representation of real-world relationships.