Python 3-Inheritance

Python Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

Ø  Parent class is the class that being inherited from, also called base class

Ø  Child class is the class that inherits from another class, also called derived class.

 

Create a Parent Class

Any class can be parent class. The syntax is same as creating a class.

Example:

Create a class named Person, with firstname and lastname properties, and a printname method:

class Person:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
    
    def printname(self):
        print(self.fname, self.lname)

p1 = Person("John", "Smith")
p1.printname()

Output:

John Smith

Create a child class

To create a class to inherits the functionality from another class, send the parent class as a parameter when creating the child class.

Example:

Create a class named Employee, which will inherit the properties and methods from the Person class:

class Employee(Person):
    pass

When you don’t want to add any properties or methods for the child class, use the pass keyword.

Now the Employee class has all the properties of Person class. For example,

p1 = Employee("John", "Smith")
p1.printname()

Output:

John Smith

Add the __init__() Function

For child class, we want to add __init__() function to the child class (instead of the pass keyword). When you add the __init__() function in the child class, we will no longer inherit the properties of parent’s __init__() function. The child’s __init__() function overrides the inheritance of the parent’s __init__() function.

Example:

class Employee(Person):
    def __init__(self, fname, lname):
        #add properties

In other ways, to keep the inheritance of the parent’s __init__() function, add a call to parent’s __init__() function.

Example:

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

Add Properties

We can add new property in __init__() function, in addition to the parent class properties.

Example:

Add a property called company name to the Employee class:

class Employee:
    def __init__(self, fname, lname):
        Person.__init__(self, fname, lname)
        self.companyname = "Microsoft"

The above parameter company name can be variable and passed into the Employee class when creating employee objects. To do so, add another parameter in the __init__() Function.

Example:

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

p1 = Employee("John", "Smith", "Google")

Add Methods

The method can be added in the Parent class and can be inherited to the child class. If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.

Example:

class Employee(Person):
    def __init__(self, fname, lname, companyname):
        Person.__init__(self, fname, lname)
        self.companyname = companyname
        
    def Welcome(self):
        print("Welcome", self.fname, self.lname, "to", self.companyname)

p1 = Employee("John", "Smith", "Google")
p1.Welcome()

Output:

Welcome John Smith to Google