Python Method Overriding

We had already discussed constructor overriding and method overriding under inheritance. When there is a method in the super class, writing the same method in the sub class so that it replaces the super class method is called 'method overriding' .

Method Overriding

The programmer overrides the super class methods when he does not want to use them in sub class. Instead, he wants a new functionality to the same method in the sub class.

Method Overriding Example

CopiedCopy Code

#method overriding 
import math 
class Square: 
   def area(self, x): 
      print('Square area= --.4f'-- x*x) 
class Circle(Square): 
   def area(self, x): 
      print('Circle area= --.4f'-- (math.pi*x*x)) 
c = Circle() 
c.area(15)
Python OOPS Points to Remember
  1. The concept of deriving new classes from the existing classes such that the new classes inherit all the members of the existing classes is called inheritance.
  2. The already existing class is called super class or base class and the newly created class is called sub class or derived class. The syntax for inheritance is: class Subclass(Baseclass):
  3. The main advantage of inheritance is code reusability. This will increase the productivity of the organization.
  4. Since the sub class contains a copy of super class, all the members of the super class are available to the sub class.
  5. The variables, methods and also constructors of the super class are available to the sub class.
  6. Writing a constructor in the sub class will override the constructor of the super class. In that case, the super class constructor will not be available.
  7. Writing a method in the sub class with the same name as that of the super class method is called method overriding. In this case, the super class method is not available. Only the sub class method is executed always
  8. super() is a method that is useful to refer to all members of the super class from a sub class. For example, super().__init__() #call super class constructor super().__init__(arguments) #call super class constructor and pass arguments super().method() #call super class method
  9. Python supports single inheritance and multiple inheritance and all other forms of inheritance which may rise from combining these two types of inheritances.
  10. Deriving one or more sub classes from a single base class is called 'single inheritance'.
  11. Deriving sub classes from multiple (or more than one) base classes is called 'multiple inheritance'.
  12. Starting from the current class, searching in parent classes in depth-first, left to right fashion without searching the same class twice is called Method Resolution Order (MRO).
  13. If a variable, object or method exhibits different behavior in different contexts, it is called polymorphism.
  14. Python follows duck typing where the type of the object is not checked while invoking the method on the object. Any object is accepted as long as that method is found in the object.
  15. If any operator performs additional actions other than what it is meant for, it is called operator overloading. Operator overloading is an example for polymorphism.
  16. Magic methods are the methods which are internal representations of operator symbols.
  17. If a method is written such that it can perform more than one task, it is called method overloading. Method overloading is an example for polymorphism.
  18. When there is a method in the super class, writing the same method in the sub class so that it replaces the super class method is called method overriding. Method overriding is an example of polymorphism.