This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jval Test : Overriding & Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jval Test : Overriding & Overloading" Watch "Jval Test : Overriding & Overloading" New topic

Jval Test : Overriding & Overloading

Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Hi there
I have so doubts about the following question
When can we use same method name in java for two different methods
A. If the two methods are in two unrelated classes.
B. If the two methods are in the same class and they differ in argument & return type.
C. If one method is in base class, other is in its subclass and the two methods differ only in return type.
D. If one method is in base class, other is in its sub class and the two methods differ both arguments list and in return type.
The answer given is A, B and D.
IMHO, when we want to use same method name for two methods it means we want to do overloading. According to my knowledge return type is not part of a method signature. Thus it is not taken account when a method is overload.
As such, statement B is not entirely correct becuase it is possible to differ in argument type only. Am I right?? Are A and D the best answer for this question.
Thanks a lot.

--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Karen
If you're not sure about answer, you can write a little code and test it.
Answer B says that, the code below will compile.
class Test{
int method(int i) { return 0; }
String method(String i) { return ""; }
It compiles fine and therefore B is true.
This is a sample of method overloding.
Jamal Hasanov
Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Hi Jamal,
I did try out some samples. I know the example you given compile fine. But want I am dubious over is the usage of the word and in answer B.
I think it's not true that to overload a method we have to differ in return type as return type is not part of a signature.
Examples shown below compile fine too and it's a valid form of overloading.
int MyMethod(int a){return 0;}
int MyMethod(String b){return 0;}
That's the reason why I post the question becuase I want to know the best answer.
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
8.4.7 Overloading
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.

In other words, definition of two methods inside 1 class, with the same name but with different return and argument type(different signature) - is called overloading.
That's why B is true.
Jamal Hasanov
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi Karen,
The question asks you when you can use the same name for two different methods. Answer (B) is correct because you can indeed use the same name in this instance. It is not asking the definition of overloading/overriding.
Consider Paul's rocket mass heater.
subject: Jval Test : Overriding & Overloading
It's not a secret anymore!