Try it here
Subscribe
Python : Inheritance , Method Overloading & Method Overriding

Python Inheritance

python_inheritance

When one object acquires all the properties and behaviors of parent object, it's known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

To make a class inherit from another, we apply the name of the base class in parentheses to the derived class’ definition.

class Employee:
    pass
class Programmer(Employee):
    pass

print(issubclass(Programmer, Employee))

True

Types of Inheritance in Python

There are five types of inheritance in python.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance

A single Python inheritance is when a single class inherits from a class.

class Programmer:
    def __init__(self, ename, esal):
        self.ename = ename
        self.esal = esal
        print('I am an Programmer')
    
    def display(self):
        print(f'Employee name : {self.ename} and Salary : {self.esal}')

class Developer(Programmer):
    def __init__(self, ename, esal, edept):
        super().__init__(ename, esal)
        self.edept = edept
        print('I am an Developer')
    
dev = Developer('Kim', 50000, 'IT')
dev.display()
print(issubclass(Developer, Programmer))

==========================================================================================
I am an Programmer
I am an Developer
Employee name : Kim and Salary : 50000
True

Here, the Developer class inherits the objects(Display function) from Programmer class.

print(issubclass(Developer, Programmer))  # Result True

Multiple Inheritance

Multiple Python inheritance are when a class inherits from multiple base classes.

class Employee:
    pass
class Programmer:
    pass
class Developer(Employee, Programmer):
    pass

print(issubclass(Developer,Employee))
print(issubclass(Developer,Programmer))
==========================================================================================
True
True

Check for More details on Multiple Inheritance

Multilevel Inheritance

When one class inherits from another, which in turn inherits from another, it is multilevel python inheritance.

class A:
    x = 5
class B(A):
    pass
class C(B):
    pass

ins_of_c = C()
print(ins_of_c.x)
==========================================================================================
5

Hierarchical Inheritance

When more than one class inherits from a class, it is hierarchical Python inheritance.

class A:
    x = 5
class B(A):
    pass
class C(A):
    pass

print(issubclass(B,A))
print(issubclass(C,A))
==========================================================================================
True
True

Hybrid Inheritance

Hybrid Python inheritance is a combination of any two kinds of inheritance.

class A:
    x = 5
class B(A):
    pass
class C(A):
    pass
class D(B, C):
    pass

d = D()
print(d.x)
==========================================================================================
5

Python Super Function

Super can be called upon in a single inheritance, in order to refer to the parent class or multiple classes without explicitly naming them. It’s somewhat of a shortcut, but more importantly, it helps keep your code maintainable for the foreseeable future.

Super can be called upon in a dynamic execution environment for multiple or collaborative inheritance. This use is considered exclusive to Python, because it’s not possible with languages that only support single inheritance or are statically compiled.

The great thing about super is that it can be used to enhance any module method. Plus, there’s no need to know the details about the base class that’s being used as an extender. The super function handles all of it for you.

So, for all intents and purposes, super is a shortcut to access a base class without having to know its type or name.

In Python 3 and above, the syntax for super is:

super().methoName(args) and for __init__() just call super()

Whereas the normal way to call super (in older builds of Python) is:

super(subClass, instance).method(args)
class Parent:
    def __init__(self, msg):
        self.msg = msg

    def display_msg(self):
        print(self.msg)

class Child(Parent):
    def __init__(self, msg):
        # super(Child, self).__init__(msg)  # Python 2
        super().__init__(msg)  # Python3 and above

c = Child('Hello, This is example of Super function')
c.display_msg()
==========================================================================================
Hello, This is example of Super function

Overriding

The redefining of the method of super class is know as Method Overriding.

The method overriding in Python means creating two methods with the same name but differ in the programming logic. The concept of Method overriding allows us to change or override the Parent Class function in the Child Class.

In Python, to override a method, you have to meet certain conditions, and they are:

  • You can’t override a method within the same class. It means you have to do it in the child class using the Inheritance concept.
  • To override the Parent Class method, you have to create a method in the Child class with the same name and the same number of parameters.
class Shape:
    def area(self, dim):
        print(f'Area of Square is : {dim ** 2}')

class Circle(Shape):
    def area(self, radi):
        super().area(radi)
        print(f'Area of Circle is : {round(3.14 * radi * radi, 2)}')

c = Circle()
c.area(3)

==========================================================================================
Area of Square is : 9
Area of Circle is : 28.26

Overloading

For overloading check Python Overloading.

Writer profile pic

Neha on Sep 20, 2020 at 11:09 am


This article is contributed by Neha. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.