Author | Topic: Overload |
Umesh ranch hand |
posted March 25, 2000 10:06 AM
Given the following class definition, which of the following methods could be legally placed after the comment //Here 1)public void amethod(String s, int i){} Ans:1)&4)
|
PK unregistered |
posted March 25, 2000 11:51 AM
Remeber - You can not override the method in the same class.
|
Jim Yingst sheriff |
posted March 25, 2000 12:01 PM
...and you can't override using a different return type. So it's illegal for two reasons.
|
Nalini Mistry ranch hand |
posted March 25, 2000 12:51 PM
here you arent talking of overriding but overloading. Overriding methods is something that can happen only in subclasses of the class concerned. To answer your qs, methods with identical signatures( signatures only include name and args )cannot have different return types. Basically if you think about it, its logical - if such a method is called, how should the decision be made?? you cannot have some calls to a method returning a value and others not. your program is going to fall apart if there is an ambiguity about whether a value will be returned or not. And java doesnt (yet) resolve method calls by looking at the calling statement to see if its expecting a return value or not!!!:-) tho this does bring a qs to my mind.
|
maha anna bartender |
posted March 25, 2000 01:04 PM
Nalini, Overloading methods in the same class or subclass are COMPLETELY NEW BORN methods. They just happened to have the same name as the overloaded method. The overloading methods can return any value, throw any kind of Checked Exception/RuntimeException/Error as they wish. regds maha anna
|
Nalini Mistry ranch hand |
posted March 26, 2000 06:12 AM
i stand corrected. Overloading is when just the names are the same but the arg types and/or nos are different. so the ret value and exceptions can be different. Overriding is when name AND arg list and type is identical, and it cannot happen in the same class. Woz stating in the context of Umesh's qs. In case of option no 2. arent we trying an (illegal) override instead of overload since the method sign is the same?? [This message has been edited by Nalini Mistry (edited March 26, 2000).]
|
Jim Yingst sheriff |
posted March 26, 2000 09:38 AM
Nalini- right, it's an illegal override rather than an overload.
|
| | |