permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need rules for overloading please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need rules for overloading please" Watch "Need rules for overloading please" New topic

Need rules for overloading please

B Barnett

Joined: Oct 03, 2000
Posts: 9
I am scheduled to take the exam on Sunday, October 15 at 1:00pm EST and wanted to clarify something. A lot of the people who have just taken the test have mentioned that there are several questions regarding overloading/overriding methods.
I have researched the JLS and the tutorial at Sun's website and also am using the Exam Cram book by Bill Brogden but I have not been able to locate a list of rules about overloading.
Can someone tell me, as concisely as possible, what conditions must exist for a method to be overloaded without causing a compiler error.
I understand that the argument list must be different. If the order of the arguments in the method is different, will it be considered overloading?
Example: public void method(String s, int i){}
public void method(int i, String s){}
//Is this considered overloading.
Please enlighten me as to what the rules are for overloading a method, whether in a class or subclass.
Thank you
- B Barnett

lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
If the order of arguments are changed,
its still considered as Overloading (according to Thinking in Java, BruceEckel)
Also, he doesnt think its a good programming practice.
[This message has been edited by lakshmi nair (edited October 13, 2000).]
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
There is not as much to remember about overloading methods as compared to overriding methods.
From RHE, an overloaded method is any method in the same class including methods inherited from a superclass with the same name but different argument lists.
They are really independent methods and return type, accessibility, and exception lists can vary any way you want.
So for you example, yes that is overloading as you are giving them a different order. The test will try to get you by having two methods and just change the return type like this:
public int methodA(String s)
private String methodA(String t)
This is not overloading and will cause a compile error because the compiler looks at the argument list and not return type. This is logical when you think about it because when calling a method, the compiler would not know which method you wanted if both took just one string value. The compiler is not going to know what the return type is going to be, so it wouldn't know where to start. But if you had one taking an int and the other taking a String, the compiler will know which method to call by the argument you pass in.
One more note, overloading is not considered Object Oriented programming, while overriding is considered OOP. Just in case you get that question also. Overriding has to do with inheritance and polymorphism while overloading is just a play on names and method calls.
All this can be found in RHE chapter 6.
Anbooo Sanygao
Ranch Hand

Joined: Oct 04, 2000
Posts: 45
One small tip. Above everything just stay calm and cool. Dont get tensed. Your mail sounds as if u have worked hard a lot all these days and this last moment b4 exam u r getting tensed and confusing and having too many things on mind. Last minute....just review whatever u hv done so far and do some tests. You will be alright. All the Best.
I agree. Here's the link:
subject: Need rules for overloading please
jQuery in Action, 3rd edition