Overloading and Overriding in C# with example

Overloading & Overriding in C# with real life example overloading what we can do overloading what we cannot do c# overriding real life example overloading real life example pros and cons of overloading and overriding difference between overloading and overriding in c# difference between overloading and overriding in .net difference between overloading and overriding in c sharp difference between overloading and overriding in c# function overloading and overriding in c# method overloading and overriding method overloading vs method overriding

In my early day career in many interviews I asked same question, what is overloading and overriding so decided to write this small article which may help someone. We will see here in detail about the overloading, overriding and then differences between them.


Overloading:

is the mechanism to have more than one method with same name but with different signature (parameters). A method can be overloaded on the basis of following properties

  1. Have different number of parameter
  2. Having same number of parameters but of different type
  3. Having same number and type of parameters but in different orders

A method cannot be overloaded on the basis of

  1. Different return type
  2. Different access modifier
  3. Normal and optional parameters

Some example valid examples:

public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
public List<Customer> FindCustomer(Int32 customer_Id)
{
  // your code
}

public List<Customer> FindCustomer(String customer_name, String city)
{
  // your code
}
public List<Customer> FindCustomer(String customer_name, Int32 customer_Id)
{
  // your code
}

public List<Customer> FindCustomer(String customer_name, String city, String zip)
{
  // your code
}

Following are invalid overloading because we can not overload on the basis of different return type

public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
public Customer FindCustomer(String customer_name)
{
  // your code
}

Following are also invalid overloading, because we cannot overload on the basis of access modifier

public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
private List<Customer> FindCustomer(String customer_name)
{
  // your code
}

Following are also not valid, because we cannot overload normal and optional parameter

public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
private List<Customer> FindCustomer(String customer_name = String.Empty)
{
  // your code
}

In the same way we can overload constructor of a class by defining more than one constructor with different parameters which is known as constructor overloading.

It is also called:

  1. Early Binding or
  2. Compile Time Polymorphism or
  3. Static binding

Overriding:

Overriding can be done in derived class, an override method provides a new implementation of a method inherited from parent class.

To override a method in base (parent) class it must be

  1. virtual
  2. abstract
  3. override

We cannot override a base method which is in base class as

  1. non-virtual
  2. static

We cannot use the following modifiers to modify an override method in derived class

  1. new
  2. static
  3. virtual
  4. abstract

Let's see it with example:

public class Square 
{
  public double side;
  // Constructor:
  public Square(double side) 
  {  this.side = x; }

  public virtual double Area() 
  {   return side  * side ;  }
}

Note Area method is defined with virtual so it can be overridden in derived class, now lets derive a class cube from square and override it's Area method

class Cube: Square 
{
   // Constructor:
   public Cube(double side): base(x)    { }
   // Calling the Area base method:
   public override double Area() 
   { return (6*(base.Area())); }
}

It is also called:

  • Late Binding or
  • Run Time Polymorphism or
  • Dynamic binding

Conclusion:

  • Overloading can be done in same class
  • Overriding can be done in parent and derived class
  • Overloading in used when we need same method in same class with different parameters
  • Overriding is used when we need to redefine a method that has already been defined in parent class (using the exact same signature
  • Overloading is resolved at compile time
  • Overriding is resolved at run time

Some commonly asked interview questions:

Q. How’s method overriding different from overloading?

When overriding, we change the method behaviour for a derived class. Overloading simply creating more than one method with the same name but different parameters in same class.

Q. Is the following code correct?

class Program
{
  public static void Main()
  {
    ......
  }
  public void Sum(int N1, int N2)
  {
    var result = N1 + N2;
  }

  public int Sum(int N1, int N2)
  {
    return N1 + N2;
  }
}

ANS: No, If you note, the different is only in the return type. We cannot overload a method, only on the basis of different return type. A method can be overloaded if it have different number of parameters, different types of parameters or sequence of the parameters which change the data type say: method(int i, string x) and other method(string x, int i), it can be overloaded.

Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • overloading
  • c#
  • oops
By Hamden On 31 Mar, 13  Viewed: 27,939

Other blogs you may like

Method overloading in C# with example

Method overloading means having different methods with same name but with different types of parameters or number of parameters also known as static polymorphism. In this article we will try to understand what is method overloading and how we can overload a method with example. Suppose we have a... By Hamden   On 19 Jul 2013  Viewed: 1,562

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 1,362

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 2,547

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 2,025

Array in C# - single, multiple and jagged array

An array can be initialized in several ways, it can store a single value or series of values, every element must be set of same type, C# supports single-dimensional arrays, multidimensional arrays (also call rectangular array) and array-of-arrays (referred jagged arrays), let’s discuss features of... By Ali Adravi   On 24 Mar 2013  Viewed: 2,356