Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Inheritance


Inheritance

A class can inherit properties and functions from another class by using inheritance.

A parent class is a class that is inherited from, also known as a base class.

A child class is a class that inherits from another class, also known as a derived class.

You can access functions and properties from the parent class through inheritance, which helps to reduce code redundancy.

Syntax
class ParentClass:
    # functions and properties of the parent class
    pass

class ChildClass(ParentClass):
    # functions and properties from the parent class
    # functions and properties of the child class
    pass

In the above class inheritance syntax, we have two classes, ParentClass and ChildClass. After declaring the ChildClass name, we added the ParentClass name in round brackets at the end of the ChildClass name.


Example
class Person:
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"The person name is {self.first_name} {self.last_name}."

class Employee(Person):
    pass

obj = Employee("John", "Matthews")
print(obj.get_person_info())
The person name is John Matthews.

In the above example, we declared the two classes Person and Employee here, the Person class is the parent class, and Employee is the child class.

Since we are performing inheritance here, we don't get an error when we create the object of the child class and call the parent class function.


Add the __init__() Function in the Child Class in Inheritance

You will not be able to inherit the parent class __init__() function when you add the __init__() function to the child class.

If you want to keep the inheritance of the parent class __init__() function, call the parent class __init__() function.

To call a parent class __init__() function to the child class, use the parent class name with the __init__() function.

Example
class Person:
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"The person name is {self.first_name} {self.last_name}."

class Employee(Person):
    def __init__(self, fname, lname, division):
        Person.__init__(self, fname, lname)
        self.work_division = division

obj = Employee("John", "Matthews", "A")
print(obj.get_person_info())
The person name is John Matthews.

Here, we added the parent class name to call the parent class to __init__() function.


A super() Function in Python

Another way to call a parent class __init__() function to the child class, use the super() function with the __init__() function.

Make sure you don't pass the child class instance to the parent class __init__() function when you call it with the super() function.

Example
class Person:
    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"The person name is {self.first_name} {self.last_name}."

class Employee(Person):
    def __init__(self, fname, lname, division):
        super().__init__(fname, lname)
        self.work_division = division

obj = Employee("John", "Matthews", "A")
print(obj.get_person_info())
The person name is John Matthews.

In the above example, we added the super() function to call the parent class __init__() function.


Using super(), Call Parent Class Variables and Functions

You can also use the super() function to call parent class variables and functions in the child class.

Example
class Person:

    message = "Welcome"

    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"{self.first_name} {self.last_name}."

class Employee(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)

        print(super().message)
        print(super().get_person_info())

obj = Employee("John", "Matthews")
Welcome
John Matthews.

Using the Class Instance, Call Parent Class Variables, and Functions

You can also use the child class instance to call parent class variables and functions in the child class.

Example
class Person:

    message = "Welcome"

    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"{self.first_name} {self.last_name}."

class Employee(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)

        print(self.message)
        print(self.get_person_info())

obj = Employee("John", "Matthews")
Welcome
John Matthews.

Using the Parent Class Name, Call Parent Class Variables, and Functions

You can also use the parent class name to call parent class variables and functions in the child class.

Example
class Person:

    message = "Welcome"

    def __init__(self, fname, lname):
        self.first_name = fname
        self.last_name = lname

    def get_person_info(self):
        return f"{self.first_name} {self.last_name}."

class Employee(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)

        print(Person.message)
        print(Person.get_person_info(self))

obj = Employee("John", "Matthews")
Welcome
John Matthews.