top of page

Inheritance In Python - Python Assignment Help

Inheritance is the capability of one class to derive or inherit the properties from another class. The benefits of inheritance are:

  1. It represents real-world relationships well.

  2. It provides reusability of a code. 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.

  3. 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.



There is no object-oriented programming language that would be qualified to take a gander at or use, on the off chance that it didn’t uphold inheritance. Inheritance was concocted in 1969 for Simula. Python upholds inheritance as well as multiple inheritances also. As a rule, inheritance is the component of getting new classes from existing ones. By doing this, we get a pecking order of classes. In many class-based OOP languages, an object made through inheritance (a “child object”) gains all, – however, there are exemptions in some programming languages, – of the properties and practices of the parent object.


Inheritance permits software engineers to make classes that are based on existing classes, and this empowers a class made through inheritance to acquire the properties and techniques for the parent class. This implies that inheritance upholds code reusability. The methods or as a rule the product acquired by a subclass is viewed as reused in the subclass. The relationships of items or classes through inheritance lead to a coordinated graph.


The class from which a class acquires is known as the parent or superclass. A class that acquires from a superclass is known as a subclass, likewise called heir class or child class. Superclasses are now and then called precursors also. There exist various hierarchical connections between classes. It’s like connections or orders that we know from reality. Ponder vehicles, for instance. Bicycles, vehicles, transports, and trucks are vehicles. Pick-ups, vans, sports vehicles, convertibles, and bequest vehicles are altogether vehicles and by being vehicles they are vehicles also. We could carry out a vehicle class in Python, which may have strategies like speed up and brake. Vehicles, Buses and Trucks, and Bikes can be carried out as subclasses that will acquire these techniques from vehicles.


Inheritance is the ability of one class to infer or acquire properties from another class. The advantages of inheritance are:

  • It addresses true connections well.

  • It gives the reusability of a code. There is no purpose in composing a similar code over and over again. Likewise, it permits us to add more elements to a class without changing it.

  • It is transitive, which implies that assuming class B acquires from another class A, every one of the subclasses of B would naturally acquire from class A.

Python Inheritance Syntax

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class

Derived class inherits features from the base class where new features can be added to it. This results in re-usability of code.

Example of Inheritance in Python

To demonstrate the use of inheritance, let us take an example. A polygon is a closed figure with 3 or more sides. Say, we have a class called Polygon defined as follows.

class Polygon:def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])

This class has data attributes to store the number of sides n and magnitude of each side as a list called Sides.

The inputSides() method takes in the magnitude of each side and dispSides() displays these side lengths.

A triangle is a polygon with 3 sides. So, we can create a class called Triangle which inherits from Polygon. This makes all the attributes of Polygon class available to the Triangle class.

We don't need to define them again (code reusability). Triangle can be defined as follows.

class Triangle(Polygon):def __init__(self):
        Polygon.__init__(self,3)

    def findArea(self):
        a, b, c = self.sides
        # calculate the semi-perimeter
        s = (a + b + c) / 2
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5print('The area of the triangle is %0.2f' %area)

However, class Triangle has a new method findArea() to find and print the area of the triangle. Here is a sample run.

>>> t = Triangle()

>>> t.inputSides()
Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4>>> t.dispSides()
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0>>> t.findArea()
The area of the triangle is 6.00

We can see that even though we did not define methods like inputSides() or dispSides() for class Triangle separately, we were able to use them.

If an attribute is not found in the class itself, the search continues to the base class. This repeats recursively, if the base class is itself derived from other classes.


If you are looking for any kind of coding help in programming languages, Contact us for instant solution





bottom of page