Inheritance And Polymorphism

Inheritance allows programmers to inherit the methods and attributes of one class to the other class.

This is really helpful when you want to use some useful methods of a class. Instead of coding it again ,we can simply derive from that class. This helps in avoiding repeated chunks of code and makes the code much more efficient.

The class from which you derive methods is called an base class while the class in which you derive methods in is called an derived class

In order to derive from the class , you simple need to specify the class name within the parenthesis of the derived class. This will become much clear from the illustration below.

how to derive from base class to derived class.

Note that once the derive class inherits from the base class , all the methods that were present inside the base class are readily accesible from the derived class.

In the code given below , we have 2 class ( Shape1 and Shape2 ) and we want to use the MyName method in Shape1 inside the Shape2 class using inheritance.

class Shape1():    def __init__(self,shape_name):      self.shape_name = shape_name    def MyName(self):      return 'I am a '+self.shape_name        class Shape2(Shape1):    def __init__(self,shape_name):       self.shape_name = shape_name          my_shape = Shape2('triangle')  print(my_shape.MyName())

output:
I am a triangle

Inheritance also makes it possible to overwrite the definition of a method. To make changes , you’ll have to basically redefine the whole method within your derived class. Say we want to make some changes ( in our derived class ) to the MyName method . So instead of returning the name of the shape , it returns the name of the class. This can be done in the following way:

 class Shape1():    def __init__(self,shape_name):      self.shape_name = shape_name    def MyName(self):      return 'I am a '+self.shape_name        class Shape2(Shape1):    def __init__(self):      pass # to avoid syntax error    def MyName(self):      return 'Shape2'          my_shape = Shape2()  print(my_shape.MyName()) 

output:
Shape2
Polymorphism

Say there are 2 separate classes that happens to share a same method name , The ability to call the same method without worrying about specific class is called Polymorphism . This will become clear from the example below.

In the example given below , we have two classes ( Color1 and Color2 ) and both of these clases have a common method name called ColorName that returns the value of color attribute. First we will be performing polymorphism using for loops and then with functions.

Polymorphism using For loop

class Color_1():    def __init__(self,color):      self.color = color    def ColorName(self):      return self.color      class Color_2():    def __init__(self,color):      self.color = color    def ColorName(self):      return self.color    my_color1 = Color_1('green')  my_color2 = Color_2('red')    for x in [my_color1,my_color2]:    print(x.ColorName())

output:
green  red

Polymorphism using functions

class Color_1():    def __init__(self,color):      self.color = color    def ColorName(self):      return self.color      class Color_2():    def __init__(self,color):      self.color = color    def ColorName(self):      return self.color    my_color1 = Color_1('green')  my_color2 = Color_2('red')    def tellcolor(instance):    print(instance.ColorName())      tellcolor(my_color1)  tellcolor(my_color2)

output:
green  red