Method Overloading And Overriding

Method Overloading

C# language provides the facility to design a program including a number of methods with the same method name and in the same scope with different parameters. Each function has a unique signature or header, which is derived from the function name, number of arguments, and their types. This technique of using a method is called  Method Overloading.

Polymorphism is one of them principles of Object Oriented Programming (OOP) that uses a method for more than one purposes. Having a number of methods with the same name, represents a single method to be used at the calling point. Thus, through method overloading, C# implements Polymorphism.

During compilation, the control seeks the best match of the types of actual parameters

With the formal parameters of the overloaded methods. The method that is found suitable is linked with method call statement.

  • In method overloading, a class has more than one method with same name but having different signature.
  • Adds or extends the behaviour of an existing method.
  • Method signature includes number and type (i.e. theirs data types) of parameters.
  • Method signature does not include method return type and method access modifier.
  • An example of complete time polymorphism since actual method calling is resolved at compile time.

Thus, method overloading is a technique of defining a number of methods with the same method name but with different types or number of formal parameters.

Need of Method Overloading

With method overloading, multiple methods can have the same name with different parameters in C# (i.e. Polymorphism). If different method names are used for similar operations, then users may face a lot of interactions and they need to mention different method names for the similar operations during their invocations. Thus, it is needed to use many methods with the same name for similar operations. By using overloading concept, you can design the programs efficiently. In fact, method overloading is more effective when user tends to achieve similar results in different ways.

Example:

static int AddMethod(int x, int y)
{
  return x + y;
}
static double AddMethod(double x, double y)
{
  return x + y;
}
static void Main(String[] args)
{
  int myNum1 = AddMethod(8, 5);
  double myNum2 = AddMethod(3.4, 5.66);
  Console.WriteLine("Int: " + myNum1);
  console.WriteLine("Double: "+ myNum2);
}

 

In the above example, we overloaded the AddMethod method to work for both int and double

 

Method Overriding

  • In method overriding, a derived class have the exactly the same signature as base class.
  • Change behaviour of an existing method in the base class
  • Unlike method overloading, here methods must have same access modifiers (public, protected and internal and not private)
  • An example of run time polymorphism since actual method calling is resolved at runtime
  • In this derived class overrides the methods defined in base class.
  • Methods have same name and same signature.
  • Virtual Keyword is used in Base Class and Override Keyword with derived class.
  • Method overriding is a feature that allows a child class to provide a specific implementation of base class method.
  • The method which is overridden by an override declaration the derived class is known as the “Overridden base method”. This override method is a new implementation of the method that is inherited from the base class.
  • A virtual method can be overridden in the derived class using the “override keyword.
  • The “virtualmodifier cannot be used with the “overridekeyword.
  • The access modifier must be the same as the base class virtual method.

 

 

Example:

using System;  
public class Animal{  
    public virtual void eat(){  
        Console.WriteLine("Eating...");  
    }  
}  
public class Dog: Animal  
{  
    public override void eat()  
    {  
        Console.WriteLine("Eating bread...");  
    }  
}  
public class TestOverriding  
{  
    public static void Main()  
    {  
        Dog d = new Dog();  
        d.eat();  
    }  
}

 

Difference between Overloading and Overriding:

 

Method Overloading Method Overriding
Overloading happens at compile time.  

Overriding happens at run time.

 

If the Static method can be overloaded, which means a class can have more than one static method of same name.  If you declare a same static method in child class it has nothing to do with same method of parent class. Static methods cannot be overridden.
Overloading is done in the same class.

 

The Overriding method requires base class and child class.
The Static binding is being used for overloaded methods. The Dynamic binding is being used for overridden methods.
Return type of method does not matter in case of method overloading, it can be same of different. Overriding method can have more specific return type.
The argument list should be different when doing method overloading. The argument list should be same in the method overriding.
Private and final methods can be overloaded.  

Private and final method cannot override.

 

Method name must be the same

 

Method name must be the same

 

Argument list must be different. Argument list must be the same.
Return type can be different. Return type can be the same type or a covariant type.
Throw clause can be different. Throws clause must not throw new checked exceptions.

Can narrow exception throw?.

Accessibility can be different. Accessibility can make it less restrictive, but not more restrictive.
Declaration context A method can be overloaded in the same class or in a subclass. Declaration context A method can only be overridden in a subclass.
Method call resolution At compile time, the declared type of the reference is used to determine which method will be executed at runtime. Method call resolution the runtime type of the reference, i.e., the type of the object referenced at runtime, determines which method is selected for execution.

Leave a Comment