Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,493 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,683 people online right now. Registration is fast and FREE... Join Now!




Overloading rules

 
Reply to this topicStart new topic

> Overloading rules, Discussion on case by case basis

bhandari
Group Icon



post 1 Feb, 2008 - 06:14 AM
Post #1


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:
CODE
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:
CODE
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:
CODE
void fix(int t) {
System.out.println("fix--one int parameters");
}


CODE
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:
CODE
int fix(long t) {
System.out.println("fix--one int parameters");
return 2;
}


CODE
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:
CODE
void fix(long t) throws IOException{
System.out.println("fix--one int parameters");
return 2;
}


CODE
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
CODE
void fix(int t) {
System.out.println("fix--one int parameters");
return 2;
}


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



and method call as
CODE

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:
CODE
void fix(int t,long u) {
System.out.println("fix--one int parameters");
return 2;
}


CODE
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
CODE
BinarySearch() { //some code }
Notice that constructors don't have any return type.
Now declare a method as:

CODE
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.



Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 07:54PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month