Python Tutorials: Inheritance In Python

Python Tutorials: An Introduction to Classes and Inheritance (in Python)
Written by Paayi Tech |01-Aug-2020 | 0 Comments | 351 Views

Inheritance is another major aspect of object-oriented programming. By the inheritance, we can pass on the functionalities of the derived class to the base class. A class that inherits from the superclass is called a base class or often child class. By the inheritance method, we can share the information with other classes without making it purely public.

Here it is to be noted that the inherited class can only access the protected data members. So, we don't have to make all things public and can hide the information following is the example of inheritance in python by which the concept of private protected and public data members can also be cleared.

class MyClass(object):

    def __init__(self):

        self.public = "public"

        self._protected = "protected"

        self.__private = "private"

 

class InheritClass(MyClass):

    def __init__(self):

        MyClass.__init__(self)

        print(f"Public data member: {MyClass().public}")

        print(f"Protected data member: {MyClass()._protected}")

        print(f"Private data member: {MyClass().__private}")

 

InheritClass()

 

We don't have to initialize the base class we can access only by a derived class. The output of the following code is as follows:

#Output

 

Public data member: public

Protected data member: protected

 

Traceback (most recent call last):

  File "/home/lalatoofani/program.py", line 15, in

    InheritClass()

  File "/home/lalatoofani/program.py", line 13, in __init__

    print(f"Private data member: {MyClass().__private}")

AttributeError: 'MyClass' object has no attribute '_InheritClass__private'

 Here we can see that private members cannot be accessed.

 

Now we will see a detailed example of inheritance. In the following class, there are three classes employee and programmer. The programmer is inherited by the employee and use the functionality of the employee class:

class Employee:

    def __init__(self,fname,lname,pay):

        self.fname = fname

        self.lname = lname

        self.pay = pay

        self.appraisal = 1.04

 

    def fullname(self):

        return f"{self.fname} {self.lname}"

    def increaseSalary(self):

        self.pay = int(float(self.pay)*self.appraisal)

 

class Programmer(Employee):

   

    def __init__(self,fname,lname,pay,language):

        super().__init__(fname,lname,pay)

        self.language = language

   

 

prog1 = Programmer('Ash','Ketchum','45000','python')

print(prog1.fullname())

prog1.increaseSalary()

print(f"Salary after increment {prog1.pay}")

 

We defined a class Employee in which it takes the first name of the employee, the last name of the employee, and the pay. There is another parameter of the appraisal. The employee class has two methods one is getting the full name, and the other is increased salary. Then we defined another class called a programmer. The programmer class inherits from the employee and take an extra parameter of language. We initialize the super constructor.

Then we create the instance pass the parameter, and by the instance of the programmer, we can access the functionalities of employees to manipulate the values.

The output of the above-given code is following;

#Output

 

Ash Ketchum

Salary after increment 46800

 

Similarly, we can do multiple inheritances and execute more than one derived class at a time. Following is the example of multiple inheritances:

class First(object):

    def __init__(self):

        super(First,self).__init__()

        print('First Class is Executed')

 

class Second(object):

    def __init__(self):

        super(Second,self).__init__()

        print('Second class is executed')

 

class Third(Second, First):

    def __init__(self):

        super(Third,self).__init__()

        print('Third Class is executed')

 

Third()

 

#Output

 

First Class is Executed

Second class is executed

Third class is executed.

 

 

 





Login/Sign Up

Comments




Related Posts



© Copyright 2020, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.