Infolink

 

Search This Blog

Feb 22, 2014

Overloading and Overriding in C# with real world example

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.

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
  •     virtual
  •     abstract
  •     override

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

  •     non-virtual
  •     static

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

  •     new
  •     static
  •     virtual
  •     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())); }
}

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

1 comment:

Related Posts Plugin for WordPress, Blogger...