Skip to main content

method overloading vs method overridingMethod Overloading

Method overloading, also known as Function overloading or Compile time polymorphism, is a concept of having two or more methods with the same name but different signature in the same scope. There are many programming languages that support this feature: Ada, C++, C#, D, and Java.

Example of Method Overloading in C#
overloadingAs shown in the above example, the method ‘Polygon’ is overloaded 3 times with different method signature, i.e the type or the number of parameters are different.

Method Overriding

Method overriding, also known as Function overriding or Run time polymorphism, is an OOP feature that allows a child class to provide its own implementation to the method defined in the parent class. The implementation in the child class overrides the definition of the method in the base class, provided that the method in the child class should have the same name, signature and return type.

Example of Method Overriding in C#
overridingHere, the draw method in the Source2 class overrides the draw method defined in the Source1 class.

Difference Between Method Overloading and Method Overriding

  1. In Method overloading methods must have a different signature. In method, overriding methods must have the same signature.
  2. Function Overloading is to “add” or “extend” more to method’s behaviour. Function overriding is to completely “change” or “redefine” the behaviour of a method.
  3. Method overloading is used to achieve Compile time polymorphism; method overriding is used to achieve run-time polymorphism.
  4. In method/function overloading compiler knows which object assigned to which class at the time of compilation, but in method overriding this information is not known till runtime.
  5. Function Overloading takes place in the same class whereas Overriding takes place in a class derived from a base class.