What is hybrid inheritance in Python?
Inheritence
-
Inheritance is the process of creating a new class from an existing class.
-
The class that is inherited is known as the super/parent/base class, and the class that inherits is known as the sub/child/derived class.
-
A derived class can access properties of the base class.
-
Inheritance provides extendibility and reusability.
Types of inheritance
- Single inheritance
- Multi-level inheritance
- Multiple inheritances
- Hierarchical inheritance
- Hybrid inheritance
Hybrid inheritance
Features of more than one type of inheritance are mixed to form Hybrid Inheritance.
Example
# Creating a Base class named University:class University:def __init__(self):print("Contructor of the Base class")# Initializing a class variable named univ to store university name:self.univ = "MIT"def display(self): # Method to print the University Name:print(f"The University name is: {self.univ}")
In the Python script above:
- We create a Base class named University using the keyword “class” with a constructor (init) that will be called when the object of the class is created (Object Instantiation).
- Next, in the Constructor we will initialize the data members, like univ, to store any value.
- Next, we have a display method to display the value of the data member.
Members of the "University" class
Parameters | Description |
__init__ | Non-parametrized constructor to initialize the data members. |
self.univ | Data Member of the class to store the name of the University. |
display() | Member Method to display the University Name. |
class Course(University):def __init__(self):# using "super" keyword to access members of the parent class having same name:print("Constructor of the Child Class 1 of Class University")University.__init__(self)self.course = "CSE"def display(self): # Method to print the Course Name:# using "super" keyword to access display method defined in the parent class:print(f"The Course name is: {self.course}")University.display(self)
- Now, we create another class named Course that is the Child/ Derived class of University.
- Here, since the Course class is the derived class of University, this class can access all the members of the parent class.
- Therefore, we use class name to access different members of the parent class, as shown in line numbers 5 and 10 in the above code snippet.
Members of the "Course" class
Parameters | Description |
__init__ | Non-parametrized constructor to initialize the data members. |
self.course | Data Member of the class to store the name of the Course. |
display() | Member Method to display the Course Name. |
# 2nd Derived or Child Class of University Class:class Branch(University):def __init__(self):print("Constructor of the Child Class 2 of Class University")self.branch = "Data Science"def display(self): # Method to print the Branch Name:print(f"The Branch name is: {self.branch}")
Similarly, we define the 2nd Derived class, named “Branch”, of the University parent class.
Members of the "Branch" class
Parameters | Description |
__init__ | Non-parametrized constructor to initialize the data members. |
self.branch | Data Member of the class to store the name of the Branch. |
display() | Member Method to display the Branch Name. |
# Derived or Child Class of Class Course and Branch:class Student(Course, Branch):def __init__(self):print("Constructor of Child class of Course and Branch is called")self.name = "Tonny"Branch.__init__(self)Course.__init__(self)def display(self):print(f"The Name of the student is: {self.name}")Branch.display(self)Course.display(self)
Now, we define the Child Class named “Student”, of classes Course and Branch, to make this Inheritance of type Hybrid.
In a similar way, we can define the class and call various members of the parent classes.
Members of the "Student" class
Parameters | Description |
__init__ | Non-parametrized constructor to initialize the data members. |
self.name | Data Member of the class to store the name of the Student. |
display() | Member Method to display the Student Name. |
Code
# Creating a Base class named University:class University:def __init__(self):print("Contructor of the Base class")# Initializing a class variable named univ to store university name:self.univ = "MIT"def display(self): # Method to print the University Name:print(f"The University name is: {self.univ}")# 1st Derived or Child Class of University Class:class Course(University):def __init__(self):# using "super" keyword to access members of the parent class having same name:print("Constructor of the Child Class 1 of Class University")University.__init__(self)self.course = "CSE"def display(self): # Method to print the Course Name:# using "super" keyword to access display method defined in the parent class:print(f"The Course name is: {self.course}")University.display(self)# 2nd Derived or Child Class of University Class:class Branch(University):def __init__(self):print("Constructor of the Child Class 2 of Class University")self.branch = "Data Science"def display(self): # Method to print the Branch Name:print(f"The Branch name is: {self.branch}")# Derived or Child Class of Class Course and Branch:class Student(Course, Branch):def __init__(self):print("Constructor of Child class of Course and Branch is called")self.name = "Tonny"Branch.__init__(self)Course.__init__(self)def display(self):print(f"The Name of the student is: {self.name}")Branch.display(self)Course.display(self)# Object Instantiation:ob = Student() # Object named ob of the class Student.print()ob.display() # Calling the display method of Student class.