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.
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
A method cannot be overloaded on the basis of
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:
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
We cannot override a base method which is in base class as
We cannot use the following modifiers to modify an override method in derived class
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:
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.
![]() |
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
|
By Hamden On 31 Mar, 13 Viewed: 34,626 |
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: 3,143
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: 2,615
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: 4,571
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: 3,219
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: 4,379