Friday, 9 October 2015

Difference between Method Overloading and Method Overriding in python

Difference Between Method Overloading and Method Overriding
Method OverloadingMethod Overriding
DefinitionIn Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
MeaningMethod Overloading means more than one method shares the same name in the class but having different signature.Method Overriding means method of base class is re-defined in the derived class having same signature.
BehaviorMethod Overloading is to “add” or “extend” more to method’s behavior.Method Overriding is to “Change” existing behavior of method.
Overloading and Overriding is a kind of polymorphism.Polymorphism means “one name, many forms”.
PolymorphismIt is a compile time polymorphism.It is a run time polymorphism.
InheritanceIt may or may not need inheritance in Method Overloading.It always requires inheritance in Method Overriding.
SignatureIn Method Overloading, methods must have different signature.In Method Overriding, methods must have same signature.
Relationship of MethodsIn Method Overloading, relationship is there between methods of same class.In Method Overriding, relationship is there between methods of super class and sub class.
CriteriaIn Method Overloading, methods have same name different signatures but in the same class.In Method Overriding, methods have same name and same signature but in the different class.
No. of ClassesMethod Overloading does not require more than one class for overloading.Method Overriding requires at least two classes for overriding.
Example
Class Add
{
   int sum(int a, int b)
   {
     return a + b;
   }
  int sum(int a)
  {
    return a + 10;
   }
}
Class A  // Super Class
{
  void display(int num)
  {
     print num ;
   }
}
//Class B inherits Class A
Class B //Sub Class
{
  void display(int num)
  {
     print num ;
   }
}

38 comments:

  1. example is c++ but required is python

    ReplyDelete
  2. Great explainataion...thanks

    ReplyDelete
  3. Very clear, although I think it’s better to use a Python example, as this is a Python blog :0

    ReplyDelete
  4. https://www.geeksforgeeks.org/python-method-overloading/

    ReplyDelete
  5. python online course in marathi

    ReplyDelete
  6. u have any python proxy ...

    ReplyDelete