Inheritance in Python

walden systems, geeks corner, programming, languages, Python, scope, local scope, global scope, hoisting, functions, variables, developer, scripting, decrypt, aes256, encrypt, pycrypto, pip, tuple, dictionary, list, file, read, write, readline, while, for, loops, break, continue, udf, functions, argument, return value, class, method, instance, object, inheritance, multiple, overriding
Python is a programming language that lets you work quickly and integrate systems more effectively.

What makes object orient programming languages so powerful is inheritance. Inheritance enable us to define a class that takes all the functionality from parent class and allows us to add more. Python supports inheritance, it even supports multiple inheritance. Classes can inherit from other classes. A class can inherit attributes and methods from another class, called the superclass or parent class. A class which inherits from a superclass is called a subclass or child class. S There is a hierarchal relationship between classes. It's similar to relationships or categorizations that we know from real life. The syntax for creating a subclass is as follows :

   class NewChildClass ( BaseClassName ) :
       pass

Single inheritance

In this article, we will go through a simple example of inheritance. We will use a polygon as a super class and will create a child class, triangle. We will define a polygon as a closed shape with straight sides. The class will have attributes that holds the number of sides and the length of each side as a list. We will then create a child class that will have an additional method to get the area inside. The triangle child class will have all the methods and attributes of the parent class so we won't have to define them again. This is the power of inheritance.

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])

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

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


Method overriding

If you notice, in the example above, both the Polygon and Triangle classes have a method __init__( ). When this happens, the method in the derived class overrides that in the base class. Generally when overriding a base method, we tend to extend the definition rather than simply replace it. We can use the built in super( ) method so we can refer to the parent class not by name, but by it's hierarchy. So in the example below, super.__init(3) is equivalent to Polygon.__init( self, 3 ).

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])

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

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

Multiple inheritance

Multiple inheritance is when a class can inherit attributes and methods from more than one parent class. This can allow programs to reduce redundancy, but it can also introduce a certain amount of complexity as well as ambiguity, so it should be done with thought to overall program design. Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. Like C++, a class can be derived from more than one base classes in Python. In multiple inheritance, the features of all the base classes are inherited into the derived class. The syntax for multiple inheritance is similar to single inheritance.

class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass