Tuesday, April 10, 2007

What is method overloading?

A method with changed formal parameters will lead to implementing method overloading.

int calculateSum(int i,int j)
float calculateSum(float i,int j)
double calculateSum(double i,int j)
float calculateSum(int i,float j)

5 comments :

Anoop on November 23, 2009 at 10:18 AM said...

http://stackoverflow.com/questions/1696314/java-different-return-types-when-overloading

Check this link. The examples that you have given here are bad. You can not change the method return types (unless the return type follows inheritance itself) while overloading. Overloading means changing the parameter types or number of parameters. If you can recall, checked exceptions in the throws clause of the methods follow the hierarchy in overriding.

Deepun on November 25, 2009 at 2:33 AM said...

It is still debated as per the post...thx for your comments Anoop.

Anonymous said...

Hi,

I tried int calculatesum()
and string calculatesum(), the compiler complains, calculatesum is already declared but when I try
int calculatesum(int i, float j) and int calculateSum(int i,int j) it works, so I am not sure how overloading really works.

charlie alpha on July 12, 2011 at 7:42 AM said...

You can still change a method return type as long as the method signature is/are different.Overloaded methods are differentiated by the number and the type of the arguments passed into the method. http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

examinationtime on November 20, 2012 at 6:46 AM said...

method overloading means, method should be same but parameter type/number of parameters and return type should be different

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker