Page 1 of 1

Overloading rules Discussion on case by case basis Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 01 February 2008 - 07:14 AM

Hi All,

Let's quickly define it in a basic way:
Overloading is using the same method name but different number/type/order parameters .



Case1:
A method named fix() is defined in a class as:
void fix() {
System.out.println("fix--no parameters");
}


This method can be overloaded, if a method with name fix() is defined in the same class but with different parameters as:
void fix(int t) {
System.out.println("fix--one parameters");
}


The compiler will not flash any error in the above case.
A method call as fix(2) will print fix--one parameters on console
But a method call as fix() will print fix--no parameters on console


Case2:
Consider the overloaded methods defined as:
void fix(int t) {
System.out.println("fix--one int parameters");
}


void fix(long t) {
System.out.println("fix--one long parameters");
}


Guess what will the call fix(2) do. Yes the int parameterized function will be called.

Case3:
Consider the methods in case2 redefined as:
int fix(long t) {
System.out.println("fix--one int parameters");
return 2;
}


void fix(long t) {
System.out.println("fix--one long parameters");
}


The above methods are not overloaded because return types don't matter. The compiler will flash an error in this case.


Case4:
Consider the methods in case2 redefined as:
void fix(long t) throws IOException{
System.out.println("fix--one int parameters");
return 2;
}


void fix(long t) throws SQLException{
System.out.println("fix--one long parameters");
}


The above methods are again not overloaded as exception declarations also don't matter while overloading.


Case5:
Consider the fix methods defined as in Case2 above
void fix(int t) {
System.out.println("fix--one int parameters");
return 2;
}


void fix(long t) {
System.out.println("fix--one long parameters");
}



and method call as
char ch=2;
s.fix(ch);



Here ch will be implicitly typecasted to match method. The casting will be done to a minimum wider types of those available in the method call. Thus in this case void fix(int t) will be called.

Case6:
void fix(int t,long u) {
System.out.println("fix--one int parameters");
return 2;
}


void fix(int t,float u) {
System.out.println("fix--one long parameters");
}


A method call fix(2,2) in this case will work but a method call fix(2,2.0) won't work because the second argument type double can't be typecasted to float or long. Thus it is the method calls that determine the ambiguity of the overloaded methods.


Now an interesting case. Suppose there is a class BinarySearch which has its constructor declared as
BinarySearch() { //some code }
Notice that constructors don't have any return type.
Now declare a method as:

int BinarySearch() {//some code }



A method call for the instance bs of class BinarySearch as bs.BinarySearch() will not call the constructor.Its a call to the second method which is a normal method. Thus the constructor names with different return types are also allowed. But that is an exception for constructor names only and not for other normal methods as seen in Case3.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1