Question - Define method overloading with example.
Answer -
Method overloading allows programmers to use multiple methods but with the same name. Every defined method within a program can be differentiated on the basis of the number and the type of method arguments. It is a concept based on polymorphism.
Method overloading can be achieved by the following:
- By changing the number of parameters in the given method
- By changing the order of parameters passed to a method
- By using different data types as the passed parameters
For example:
public class Methodoveloading
{
public int sum(int a, int b) //two int type Parameters method
{
return a + b;
}
public int sum(int a, int b,int c) //three int type Parameters with same method same as above
{
return a + b+c;
}
public float sum(float a, float b,float c,float d) //four float type Parameters with same method same as above two method
{
return a + b+c+d;
}
}