Inheritance in Python is a mechanism that allows a new class to inherit properties and methods from an existing class. This helps in reducing code duplication and making the code more organized and easier to maintain. In this tutorial, we will learn about inheritance in Python.
Inheritance is a mechanism that allows a new class to inherit properties and methods from an existing class. This means that the new class can use all the attributes and methods of the existing class and can add new attributes and methods to it.
In Python, inheritance is achieved using the class keyword. The new class is defined by inheriting from an existing class using the syntax class NewClass(ExistingClass). The existing class is known as the parent class or superclass and the new class is known as the child class or subclass.
class SuperClass:
# properties and methods
class SubClass(SuperClass):
# properties and methodsThe child class can access all the properties and methods of the parent class, including those that have been declared private (using double underscores). The child class can also override or extend the properties and methods of the parent class as per its requirements.
There are five types of inheritance in Python:
Single inheritance is the simplest form of inheritance in Python. A new class is created that inherits the attributes and methods of the existing class.
class Parent:
def __init__(self, name, age):
self.name = name
self.age = age
def display_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
class Child(Parent):
def __init__(self, name, age, school):
super().__init__(name, age)
self.school = school
def display_details(self):
super().display_details()
print(f"School: {self.school}")
c = Child("John", 12, "ABC School")
c.display_details()Name: John
Age: 12
School: ABC SchoolMultiple inheritance allows a new class to inherit from multiple existing classes. This means that the new class can use the attributes and methods of multiple classes.
class Father:
def __init__(self, f_name, f_age):
self.name = f_name
self.age = f_age
def display_details(self):
print(f"Father's Name: {self.name}")
print(f"Father's Age: {self.age}")
class Mother:
def __init__(self, m_name, m_age):
self.name = m_name
self.age = m_age
def display_details(self):
print(f"Mother's Name: {self.name}")
print(f"Mother's Age: {self.age}")
class Child(Father, Mother):
def __init__(self, f_name, f_age, m_name, m_age, c_name, c_age):
Father.__init__(self, f_name, f_age)
Mother.__init__(self, m_name, m_age)
self.name = c_name
self.age = c_age
def display_details(self):
Father.display_details(self)
Mother.display_details(self)
print(f"Child's Name: {self.name}")
print(f"Child's Age: {self.age}")
c = Child("Jack", 45, "Jill", 40, "John", 12)
c.display_details()Father's Name: Jack
Father's Age: 45
Mother's Name: Jill
Mother's Age: 40
Child's Name: John
Child's Age: 12Multi-level inheritance allows a new class to inherit from a class that has already inherited from another class.
class GrandParent:
def __init__(self, name, age):
self.name = name
self.age = age
def display_details(self):
print(f"GrandParent's Name: {self.name}")
print(f"GrandParent's Age: {self.age}")
class Parent(GrandParent):
def __init__(self, name, age, occupation):
super().__init__(name, age)
self.occupation = occupation
def display_details(self):
super().display_details()
print(f"Parent's Occupation: {self.occupation}")
class Child(Parent):
def __init__(self, name, age, occupation, school):
super().__init__(name, age, occupation)
self.school = school
def display_details(self):
super().display_details()
print(f"Child's School: {self.school}")
c = Child("John", 12, "Teacher", "ABC School")
c.display_details()GrandParent's Name: John
GrandParent's Age: 12
Parent's Occupation: Teacher
Child's School: ABC SchoolHierarchical Inheritance allows multiple new classes to inherit from a single existing class.
class Parent:
def __init__(self, name, age):
self.name = name
self.age = age
def display_details(self):
print(f"Parent's Name: {self.name}")
print(f"Parent's Age: {self.age}")
class Child1(Parent):
def __init__(self, name, age, school):
super().__init__(name, age)
self.school = school
def display_details(self):
super().display_details()
print(f"Child1's School: {self.school}")
class Child2(Parent):
def __init__(self, name, age, college):
super().__init__(name, age)
self.college = college
def display_details(self):
super().display_details()
print(f"Child2's College: {self.college}")
c1 = Child1("John", 12, "ABC School")
c1.display_details()
c2 = Child2("Jane", 15, "XYZ College")
c2.display_details()Parent's Name: John
Parent's Age: 12
Child1's School: ABC School
Parent's Name: Jane
Parent's Age: 15
Child2's College: XYZ CollegeHybrid Inheritance combines two or more types of inheritance in a single program.
class GrandParent:
def __init__(self, name, age):
self.name = name
self.age = age
def display_details(self):
print(f"GrandParent's Name: {self.name}")
print(f"GrandParent's Age: {self.age}")
class Parent(GrandParent):
def __init__(self, name, age, occupation):
super().__init__(name, age)
self.occupation = occupation
def display_details(self):
super().display_details()
print(f"Parent's Occupation: {self.occupation}")
class Child1(Parent):
def __init__(self, name, age, occupation, school):
super().__init__(name, age, occupation)
self.school = school
def display_details(self):
super().display_details()
print(f"Child1's School: {self.school}")
class Child2(Parent):
def __init__(self, name, age, occupation, college):
super().__init__(name, age, occupation)
self.college = college
def display_details(self):
super().display_details()
print(f"Child2's College: {self.college}")
c1 = Child1("John", 12, "Teacher", "ABC School")
c1.display_details()
c2 = Child2("Jane", 15, "Doctor", "XYZ College")
c2.display_details()GrandParent's Name: John
GrandParent's Age: 12
Parent's Occupation: Teacher
Child1's School: ABC School
GrandParent's Name: Jane
GrandParent's Age: 15
Parent's Occupation: Doctor
Child2's College: XYZ CollegeIn conclusion, inheritance is an important concept in object-oriented programming that allows a new class to inherit properties and methods from an existing class. With inheritance, you can create new classes with added or modified properties and methods without having to rewrite all the code from the existing class.