Nerd Tutorials

Explain the concept of inheritance in Python with suitable examples.

Inheritance is the capability of one class to derive or inherit the properties from another class. The benefits of inheritance are: 
 

  1. It represents real-world relationships well.
  2. It provides reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
  3. It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.

It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.

Python Inheritance Syntax

class BaseClass:
  Body of base class
class DerivedClass(BaseClass list):
  Body of derived class

Single Inheritance: Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code

# Python program to demonstrate
# single inheritance


# Base class
class Parent:
def func1(self):
print("This function is in parent class.")


# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")


# Driver's code
object = Child()
object.func1()
object.func2()
This function is in parent class.
This function is in child class.

Multiple Inheritance: When a class can be derived from more than one base class this type of inheritance is called multiple inheritance. In multiple inheritance, all the features of the base classes are inherited into the derived class. 
 

Example

# Python program to demonstrate
# multiple inheritance


# Base class1
class Mother:
    mothername = ""

    def mother(self):
        print(self.mothername)


# Base class2
class Father:
    fathername = ""

    def father(self):
        print(self.fathername)


# Derived class
class Son(Mother, Father):
    def parents(self):
        print("Father :", self.fathername)
        print("Mother :", self.mothername)


# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()

Output:

Father : RAM
Mother : SITA

Multilevel Inheritance 
In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and grandfather. 
 

Example:

# Python program to demonstrate
# multilevel inheritance

# Base class
class Grandfather:

    def __init__(self, grandfathername):
        self.grandfathername = grandfathername


# Intermediate class
class Father(Grandfather):
    def __init__(self, fathername, grandfathername):
        self.fathername = fathername

        # invoking constructor of Grandfather class
        Grandfather.__init__(self, grandfathername)


# Derived class
class Son(Father):
    def __init__(self, sonname, fathername, grandfathername):
        self.sonname = sonname

        # invoking constructor of Father class
        Father.__init__(self, fathername, grandfathername)

    def print_name(self):
        print('Grandfather name :', self.grandfathername)
        print("Father name :", self.fathername)
        print("Son name :", self.sonname)


#  Driver code
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()

Output:

Lal mani
Grandfather name : Lal mani
Father name : Rampal
Son name : Prince

Hierarchical Inheritance: When more than one derived classes are created from a single base this type of inheritance is called hierarchical inheritance. In this program, we have a parent (base) class and two child (derived) classes.
 

Example:

# Python program to demonstrate
# Hierarchical inheritance


# Base class
class Parent:
    def func1(self):
        print("This function is in parent class.")


# Derived class1
class Child1(Parent):
    def func2(self):
        print("This function is in child 1.")


# Derivied class2
class Child2(Parent):
    def func3(self):
        print("This function is in child 2.")


# Driver's code
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()

Output:

This function is in parent class.
This function is in child 1.
This function is in parent class.
This function is in child 2.

Hybrid Inheritance: Inheritance consisting of multiple types of inheritance is called hybrid inheritance.

# Python program to demonstrate
# hybrid inheritance


class School:
    def func1(self):
        print("This function is in school.")


class Student1(School):
    def func2(self):
        print("This function is in student 1. ")


class Student2(School):
    def func3(self):
        print("This function is in student 2.")


class Student3(Student1, School):
    def func4(self):
        print("This function is in student 3.")


# Driver's code
object = Student3()
object.func1()
object.func2()

Output:

This function is in school.
This function is in student 1.

invoking the Base Class Constructor

class Student:
    def __init__(self, sid, sname):
        self.sid = sid
        self.sname = sname

    def showStudent(self):
        print("Id number of the Student: ", self.sid)
        print("Name of the Student: ", self.sname)


class Teacher():
    def __init__(self, tid, tname, subject):
        self.tid = tid
        self.tname = tname
        self.subject = subject

    def showTeacher(self):
        print("Id number of the Teacher: ", self.tid)
        print("Name of the Teacher: ", self.tname)
        print("Subject: ", self.subject)


class School(Student, Teacher):
    def __init__(self, sid, sname, tid, tname, subject, schid):
        Student.__init__(self, sid, sname)
        Teacher.__init__(self, tid, tname, subject)
        #super().__init__(fname, lname)
        self.schid = schid

    def showSchool(self):
        print("Id of the School: ", self.schid)


# main program

sc = School("S1", "Jack", "T1", "John", "Python", "GECTCR")
sc.showStudent()
sc.showTeacher()
sc.showSchool()
Exit mobile version