Nearby lessons
107 of 159Python - Inheritance
- Understand the concept of inheritance
- Know the different types of inheritance
- Use super() to call parent class methods
- Implement method overriding in a child class
- Implement constructor overriding and call super().__init__()
What is Inheritance?
Inheritance is one of the fundamental concepts of Object-Oriented Programming.
Whatever members are available in the Parent Class (also called Base or Super Class) are automatically available to the Child Class (also called Derived or Sub Class).
This concept of reusing the parent class members in the child class is called Inheritance.
Why Do We Need Inheritance?
- Code reuse: existing code can be used again in a child class.
- Reduces code duplication.
- Improves code maintainability.
- Represents real-world relationships (a child is a parent).
Syntax
Explanation
The child class is defined by placing the parent class name inside parentheses:
Types of Inheritance
Python supports several types of inheritance.
| Type | Description |
|---|---|
| Single Inheritance | One child class inherits from one parent class. |
| Multilevel Inheritance | A class is inherited from another derived class. |
| Hierarchical Inheritance | Multiple child classes inherit from one parent class. |
| Multiple Inheritance | One child class inherits from more than one parent class. |
Example of Single Inheritance
Explanation
Here, Dog inherits from Animal. Because of inheritance, the Dog object can call the eat() method defined in the parent class.
What is Method Overriding?
Whatever members are available in the Parent Class are automatically available to the Child Class through inheritance.
Sometimes, the child class may not be satisfied with the implementation provided by the parent class.
In that situation, the child class can redefine the same method according to its own requirement.
This concept is called Method Overriding.
When a child class provides its own implementation of a method that is already available in the parent class, it is called Method Overriding.
The overriding concept is applicable to both:
- Methods
- Constructors
Demo Program for Method Overriding
Explanation
- The child class
Cinherits bothproperty()andmarry()fromP. property()is not overridden, so the parent implementation executes:Gold+Land+Cash+Power.marry()is overridden in the child class, so the child implementation executes:Katrina Kaif.
Calling a Parent Class Method from the Overriding Method
Normally, after overriding a method, calling that method through the child object executes the child implementation.
But sometimes we want to execute both the parent class implementation and the child class implementation.
For this purpose, we can use super().
From the overriding method of the child class, we can call the parent class method by using:
What is super()?
super() provides a way to delegate method calls according to Python's method resolution order.
In this simple single-inheritance example, it allows the child class to call the corresponding parent class implementation.
Syntax:
Demo Program Using super()
Explanation
c.property(): not overridden, so the parent implementation printsGold+Land+Cash+Power.c.marry(): the child method starts,super().marry()calls the parent implementation first, printingAppalamma.- Execution returns to the child method, which prints
Katrina Kaif.
How super() Changes the Behaviour
| Child marry() Implementation | Output of c.marry() |
|---|---|
def marry(self):
print('Katrina Kaif')
|
Katrina Kaif
|
def marry(self):
super().marry()
print('Katrina Kaif')
|
Appalamma
Katrina Kaif
|
Without super(), only the child implementation executes. With super().marry(), the inherited implementation is called first and then the remaining child code executes.
What is Constructor Overriding?
The overriding concept is applicable not only to normal methods but also to constructors.
If a child class defines its own __init__() constructor, then that constructor is used when creating objects of the child class.
This concept is called Constructor Overriding.
If the child class defines its own constructor, the parent class constructor is not executed automatically.
Demo Program for Constructor Overriding
Explanation
When the child object is created, Python finds __init__() in class C, so the child constructor executes.
The parent constructor is not executed automatically.
If the child class does not define a constructor, the parent class constructor is inherited and executes.
Calling the Parent Class Constructor Using super()
If we want to execute the parent class constructor from the child class constructor, we can use super().
Demo Program Using super().__init__()
Explanation
- Inside the child constructor,
super().__init__()calls the parent constructor first, printingParent Constructor. - Control returns to the child constructor, which prints
Child Constructor.
Without super().__init__() vs With super().__init__()
| Situation | Child Constructor | Output |
|---|---|---|
Without super().__init__() |
def __init__(self):
print('Child Constructor')
|
Child Constructor
|
With super().__init__() |
def __init__(self):
super().__init__()
print('Child Constructor')
|
Parent Constructor
Child Constructor
|
Complete Program: Reusing the Parent Constructor
This program shows the most common use of super().__init__(): the child class constructor reuses the parent constructor to initialize inherited data, then initializes its own data.
Explanation
super().__init__(name, age)calls the parent constructor, which initializesself.nameandself.age.- The child constructor then initializes the child-specific variables
self.enoandself.esal. - Without
super().__init__(),nameandagewould never be initialized, anddisplay()would raise an AttributeError.
Method Overriding vs Constructor Overriding
| Feature | Method Overriding | Constructor Overriding |
|---|---|---|
| What is Redefined? | Normal inherited method | __init__() |
| Inheritance Required | Yes | Yes |
| Child Provides Own Implementation | Yes | Yes |
| Inherited Implementation Executes Automatically? | No, when the overridden method is called | No, when child defines its own constructor |
| Call Inherited Implementation | super().method() |
super().__init__() |
| Main Purpose | Customize inherited behaviour | Customize child-object initialization |
- Inheritance allows a child class to reuse the members of a parent class
- The child class inherits all accessible members of the parent class
- Method overriding lets a child redefine an inherited method
- super() calls a parent class method or constructor from a child class
- The parent class constructor is not called automatically when the child defines its own