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

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.


A Python program to override the super class method in sub class.


Summary


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

  • 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):

  • The main advantage of inheritance is code reusability. This will increase the productivity of the organization.

  • Since the sub class contains a copy of super class, all the members of the super class are available to the sub class.

  • The variables, methods and also constructors of the super class are available to the sub class.

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

  • 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

  • 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

  • Python supports single inheritance and multiple inheritance and all other forms of inheritance which may rise from combining these two types of inheritances.

  • Deriving one or more sub classes from a single base class is called 'single inheritance'.

  • Deriving sub classes from multiple (or more than one) base classes is called 'multiple inheritance'.

  • 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).

  • If a variable, object or method exhibits different behavior in different contexts, it is called polymorphism.

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

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

  • Magic methods are the methods which are internal representations of operator symbols.

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

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