In this tutorial we are going to learn :
- Inheritance in Python
- Method Overriding
- Multiple Inheritance
Inheritance in Python
If we have more than one class and we want to access all the methods and properties from another class then we can use inheritance.
Parent class : is the class being inherited from.
Child class : is the class that inherits from another class.
Let’s explain something :
- Cat and Bird both eat. Instead of creating eat() methods for each one of them, we create one time in Animal class and we inherit from Animal class.
Child class can only access methods and variables from inside the class. Method overriding allows child class to access parent class variables from outside.
- We use the super() method to method override.
As you can see, child class can access parent class variables.
We can also inherit from multiple classes.
In this program, you have to be careful about the method name. If we inherit from multiple classes and they have methods with the same name, the child class will inherit the first class here. As you can see first class is Student here so Human class inherits from Student class first.