Wednesday, 6 April 2016

What are the different ways a method can be overloaded?

The different ways to overload a method are:-
  • By using different data types for the parameters
  • By changing the number of parameters used
  • By changing the order of parameters

1. Different parameter data types:

Example:

class MOverloading
{
public void show(int a)
{
//Statements
}
public void show(float a)
{
//Statements
}
}



In the above example show() method is overloaded based on the data type of parameters.

2. Different number of parameters:

When methods or functions name are same but number of parameters or arguments are different.

Example:

Class MOverloading
{
public void show(int a)
{
//Statements
}
public void show(int a, float b)
{
//Statements
}
}



In the above example show() has been overloaded based on the number of arguments.

3. Different order of parameters:

In this, the overloading is based on the order of the data type of parameters.

Example:
Class MOverloading
{
public void show(int a, float b)
{
//Statements
}
public void show(float b, int a)
{
//Statements
}
}

In the above example, both the methods have different sequence or order of data type in parameter list. First method is having parameter list as (int, float) and second is having (float, int). So the order is different, the method can be overloaded without any issues.



No comments:

Post a Comment

Featured post

What is SharePoint?

Microsoft SharePoint is an extensible platform that provides a range of products that can help organizations with solution for a variety...