Question

Java: What is method overloading? An example would be greatly appreciated.

Java: What is method overloading?

An example would be greatly appreciated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Method Overloading:

The method overloading means we can have multiple methods with the same name but the number of arguments or the types of arguments must be different.

Example 1:

Suppose the below two methods belong to the same class.

The first method will take two integer arguments as input and will return the sum of integer arguments.

The second method will take two float arguments as input and will return the sum of float argument.

int add(int a, int b)
{
int sum;
sum = a+b;
return sum;
}

float add(float a, float b)
{
float sum;
sum = a+b;
return sum;
}

Both methods have the same name but the type of argument is different. This is known as method overloading.

Example 2:

Suppose the below two methods belong to the same class.

The first method will take two integer arguments as input and will return the sum of two integer arguments.

The second method will take three integer arguments as input and will return the sum of three integer arguments.

int add(int a, int b)
{
int sum;
sum = a+b;
return sum;
}

int add(int a, int b, int c)
{
int sum;
sum = a+b+c;
return sum;
}


Both methods have the same name but the number of arguments is different. This is known as method overloading.

Add a comment
Know the answer?
Add Answer to:
Java: What is method overloading? An example would be greatly appreciated.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT