Inheritance in Python

  • By
  • March 26, 2022
  • Python

Inheritance in Python

OOP concepts Inheritance in python is implemented in which one object acquires  all the properties and behaviors of a parent object. It is a significant part of OOP(Object Oriented programming system). 

The idea behind inheritance in python is that you can create new classes that are  built upon existing classes. When you inherit from an existing class, you can  handle methods and fields of the parent class. Moreover, you can append new  methods and fields in your existing class also. 

Stints used in Inheritance: 

o Class A class is a bunch of objects which have common parcels. It’s a  template or design from which objects are created. 

o Sub Class/ kid Class Subclass is a class which inherits the different class.  It’s also called a derived class or kid class. 

o Super Class/ Parent Class Superclass is the class from where a class  inherits the features. It’s also called a base class or a parent class. o Reusability As the name specifies, reusability is a technique which  facilitates you to recycle the fields and methods of the being class when  you produce a new class. You can use the same fields and methods  previously defined in the former class.

For Free, Demo classes Call: 7507414653
Registration Link: Click Here!

Types of inheritance in python : 

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

Single Inheritance :  

If a class is derived from one base class(parent class),it is called single  inheritance. 

Super Class/Parent Class/base class 

Sub Class/Child Class/derived class

Syntax : 

class Superclass-name: 

//methods and fields  

class Subclass-name (Superclass-name ): 

 //methods and fields  

In the lexicon of python, a class which is inherited is called a parent or superclass,  and the new class is called kid or subclass. 

 

 

Multilevel Inheritance : 

The class inherits the feature of another derived class then that type of  inheritance is called multilevel inheritance. 

Super Class/Parent Class 

Sub Class1/Child Class1 

Sub Class2/Child Class2

Syntax : 

class Superclass-name: 

//methods and fields  

class Subclass1-name (Superclass-name):  

 //methods and fields  

class Subclass2-name (Subclass1-name ): 

 //methods and fields 

 

 

Hierarchical Inheritance : 

When base class is derived from more than one derived class then that type  of inheritance is called hierarchical inheritance.

For Free, Demo classes Call: 7507414653
Registration Link: Click Here!

Super Class/Parent Class 

Sub Class1/Child Class1 Sub Class2/Child Class2 Sub Class3/Child Class3

Syntax : 

class Superclass-name: 

//methods and fields  

class Subclass1-name(Superclass-name ): 

 //methods and fields  

class Subclass2-name (Superclass-name):  

 //methods and fields 

class Subclass3-name (Superclass-name): 

 //methods and fields 

 

 

Multiple Inheritance : 

When a class inhertis more than one class we can call it multiple  inheritance. 

For Free, Demo classes Call: 7507414653
Registration Link: Click Here!

Super Class/Parent Class 

Syntax : 

 class Superclass-name: 

//methods and fields  

 class Superclass-name1: 

 //methods and fields  

Super Class/Parent Class

Sub Class2/Child Class2 

class Subclass1-name (Superclass-name, Superclass-name1):  

 //methods and fields 

 

 

Hybrid Inheritance : 

It is the combination of single ,multiple and multilevel inheritance is called  Hybrid Inheritance.

 

Demand of Inheritance : 

It helps in degraded code. 

The most important use is the reusability of code.The code that is present  in the parent class does not need to be written in the kid class. It enhances readability of code. 

Implementation of code is productive. 

To achieve runtime polymorphism through method overriding. Benefits of inheritance : 

Software Reusability : 

The child class directly used the methods written in parent class.Thus in object  oriented programming methods can be written formely and can be reused. 

Minimize the measure of duplicate code in an application: 

If duplicate code (variable and methods) exists in two related classes, we can  refactored that hierarchy by repositioning that common code up to the common  superclass.

Code more flexible change : 

Inheritance can also make application code more elastic to change because  classes that inherit from a common superclass can be used interchangeably. If  the return type of a method is superclass.Also time consuming task. 

Cost effective: 

Inheritance provides a clear model structure which is easy to understand and  leads to less development and maintainance costs. 

Security : 

Inheritance base class can decide to keep some data private so that it can’t be  altered by the derived class. 

For Free, Demo classes Call: 7507414653
Registration Link: Click Here!

Costs of inheritance : 

Inheritance decreases the implementation speed due to the increased time  and effort it takes the program to jump through all the levels of overloaded  classes. 

Inheritance makes the two classes (base and derived class)get tightly  coupled. This means one cannot be used singly. 

The changes made in the parent class will affect the address of child class  too. 

The overuse of inheritance makes the program more complicated. Due to overuse of inheritance the program difficulty gets increased. In  order to understand the control flow of the program.

 

Author:-

Dipali Shinkar

Call the Trainer and Book your free demo Class  Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*