• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Need rules for overloading please

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings:
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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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).]
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Bill
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barnett,
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic