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.
Features of more than one type of inheritance are mixed to form Hybrid Inheritance.
# 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:
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)
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.
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.
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. |
# 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.