Understanding Inheritance & Polymorphism
Let’s understand this with an Example, when a programmer develops a class, he also develops an instance of it, whenever he wants to use this class feature. When an another developer also wants to use the feature similar to it, then he doesn’t need to create a different class from scratch.
Deriving a new class from the existing class so that new class inherits all members and its functions of existing class, is called Inheritance.
Let’s understand this by a Python program
Example 1: Write a Python program to create EMPclass and store it in EMPclass.py module.
When programmer wants to use this class, that is under EMPclass.py file, Programmer needs to simply import this class in to his/her new program names as EMP.py.
Refer below example to understand this program.
Above output when EMP.py is run, shows the use of EMPclass under EMP Class. Once the EMPclass is completed, the programmer stored EMPclass.py program in a central database that is available to all members of the team.
Now let’s understand this by another program, where a class called student is created by a programmer and save it by student.py to use its feature in his/her subsequent program.
Example 2: Write a Python program to create student class and store it in student.py module and then use this class in second Python program.
Let’s understand this by an example, where a super class by name father.py is created and is derived by sub class son.py. The super class – father.py has a constructor, under which a variable ‘property’ is declared and initialized with 80000.00/-. Now when Son class is created from father class, this constructor is by default available to Son class. Now method of super class using sub class object, it will display value of property variable.
Example 3: Write a Python program to access base class constructor from sub class.
Overriding Super Class Constructor & Method
Now when a programmer creates a Constructor in the sub class, super class constructor is not available to sub class. In this case, only sub class constructor is accessible from sub class object.
Here, sub class constructor is replacing the super class constructor and is called constructor overloading. Similarly, in sub class, if we program a method with exactly same name as that of super class, it will override the super class method and is called method overloading.
Example 4: Write a python program to override super class constructor and method in sub class.
In the above program, we created a constructor and a method with same name as those of super class. Now we refer them, only sub class constructor and method are executed, which means base class constructor and method are overridden.
Super () Method
Super (), is built-in method, useful to call super class constructor or methods from sub class. IF any constructor written in super class is not available to sub class, if sub class has a constructor and if we want to use super class constructor, then it can be done by calling the super class constructor by using super () method inside the sub class constructor.
Below is some method, by which super () is used in various ways.
- Super().__init__() # Call Super Class Constructor
- Super().__init__(arguments) # Call superclass constructor and pass
- Super().method() # Call super class method.
To understand it, let’s write a program to calculate area of square and rectangle. Now a Square class with instance x is used to calculate the area of square. Another class Rectangle is derived from Square, so to do this, value of x is inherited by rectangle class from square class. And to calculate area of rectangle, we need two values x, y. so we will take constructor with two parameter x and y in sub class.
Example 5: Write a python program to access base class constructor and method in the sub class using super ().
When a variable store different types of data, an object may exhibit different behaviors in different context or a method may perform various tasks in python, this type of behavior is called as polymorphism.
Below are some example of Polymorphism.
- Operator Overloading
- Method Overloading
- Method overriding
When an operator performs different types of actions, it is said to be Operator overloading.
Les understand this by an example.
Example 6: Write a program to use addition operator to act on different types of objects.
In above example, + operator is performing different actions other than what it is meant for.
Example 7: Write a python program to overload the addition operator (+) to make it work on class objects.
When a method is used for more than one task, it is called as method overloading.
Example 8: Write a python program to use method overloading to find sum of two or three numbers.
In this, sum () method is used to calculate sum of two or three numbers and hence is doing one or more task.
When there is a method in super class and if same method is used in sub class, due to which it replaces the super class method is called as method overriding.
Example 9: Write a python program to over ride super class method in cub class.