Python Inheritance Tutorial

  • date 17th July, 2019 |
  • by Prwatech |
  • 0 Comments

Python Inheritance Tutorial

 

Python Inheritance Tutorial, In this tutorial, we will learn What is inheritance in Python and Types of inheritance in Python. Here, You will also learn Python inheritance Syntax, an example of python inheritance which is helpful for any Python developers. Are you looking for the information on Python inheritance tutorial or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your online python course with certificate from India’s Leading Python training institute.

Inheritance provides reusability & supports transitivity. It offers faster development time, easier maintenance, and easy to extend and represents real world relationships. Do you want to know about what is inheritance in Python and Types of Inheritance in Python, then just follow the below mentioned Python Inheritance Tutorial for Beginners from Prwatech and take an advanced online course to learn python like a Pro from today itself under 10+ years of hands-on experienced Professionals.

To get a better understanding of this concept, let’s start Inheritance in Python Tutorial.

What is Inheritance in Python?

Inheritance is the ability of one class to run or inherit the properties from another class. Inheritance is the fundamental feature of OOPs that varieties the functionality of the current class by adding new features. The benefits of inheritance are:

It represents real-world relationships well. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it. Mean the reusability of code is possible.

It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A. Inheritance is used to define a new class having all the properties of the old existing class.

The parent class is the class being inherited from, also called the superclass or the base class. The child class is the class that inherits from another class, also called subclass, or derived class.

Create a Child Class

A child class needs to identify its parent class. This can be done by writing the parent class name in the definition of the child’s class.

Syntax

classclassName(parentClassName):

Example

class xyz:

print(‘xyz is parent class’)

classabc(xyz):

print(‘abc is child class of xyz’)

Output:

xyz is parent class

abc is child class of xyz

Here abc is the child class or subclass and xyz is the parent class or superclass. Now let’s see one more example which will show how the attributes and functions can be inherited by child class from the parent class.

Example of Python inheritance

Let’s consider a ‘Sports’ category is parent class and ‘Shoes’ is child or subclass in following example.

class Sports:

def __init__(self, model, color, cost):

self.__model = model

self.__color = color

self.__cost = cost

# __model,__color,__costt are private to Sports class

defgetModel(self):

# It is accessible outside the class Sports

returnself.__model

defgetcolor(self):

return self.__color

defsetcost(self, color):

self.__color = color

defgetcost(self):

returnself.__cost

defsetcost(self, cost):

self.__cost = cost

class Shoes(Sports):

def __init__(self, model,color,cost,brand):

# We have to call parent constructor to set parameters excluding brand.

super().__init__(model, color, cost)

self.__brand = brand

defShoesinfo(self):

returnself.getModel() + ” of brand “+ self.__brand + ” in ” + self.getcolor() + ” color for ” + self.getcost() + ” ”

# In method getInfo we can call getmodel(), getcolor(),getcost() as they are accessible in the child class through inheritance.

sh1 = Shoes(“Shoes”, “Black”, “Rs.3699″,”Xyz123”)

print(sh1.Shoesinfo())

print(sh1.getModel())

sh2= Shoes(“Shoes”, “Black-red”, “Rs.4599”, “Xyz345”)

print(sh2.Shoesinfo())

Output:

Shoes of brand Xyz123 in Black color for Rs.3699

Shoes of brand Xyz345 in Black-red color for Rs.4599.

Use of Super() :

The super() method permits to access the inherited methods which link to a class object. In the above example, we’ve used the super() method in the constructor of the subclass i.e. child class ‘Shoes’. It is appealing to the function of the base class ‘Sports’.

Working of super():

As we know in the above example subclass ‘Shoes’ has no properties defined such as color, cost, and model. But these are available in ‘Sports’. We can access those properties for child class Shoes with help of the super() function. Shoes have no method getModel() but it is accessible via Shoe class.

Types of Inheritance in python

Single inheritance: If a subclass inherits from only one superclass, it is called single inheritance. (As stated in the above example)

Multiple inheritances: When a subclass inherits from multiple parent classes, it is called as multiple inheritances.Python has provisions for multiple inheritances. We have to specify all parent classes in the bracket as a list with comma separation.

Multiple inheritance Example

class super1(object):

def __init__(self):

self.test1 = “Python”

print (“Superclass1”)

class super2(object):

def __init__(self):

self.test2 = “Tutorials”

print (“Superclass2”)

classmultiinher(super1, super2):

def __init__(self):

# Calling constructors of super1 and super2 classes

super1.__init__(self)

super2.__init__(self)

print (“Derived”)

defprintvals(self):

print(self.test1, self.test2)

obj = multiinher()

obj.printvals()

Output

Superclass1

Superclass2

Derived

Python Tutorials

We hope you understand Python Inheritance Tutorial concepts. Get success in your career as a Python developer by being a part of the Prwatech, India’s leading Python training institute in Bangalore.

0
0

Quick Support

image image