There is a qn from Marcus Green Which of the following methods can be legally inserted in place of the comment //Method Here ? class Base{ public void amethod(int i) { } } public class Scope extends Base{ public static void main(String argv[]){ } //Method Here } 1) void amethod(int i) throws Exception {} 2) void amethod(long i)throws Exception {} 3) void amethod(long i){} 4) public void amethod(int i) throws Exception {} Correct ans are 2 & 3.Now my question is if I call Scope s=new Scope(); s.amethod(10) ; //any integer this will give as duplicate methos declaration error.So I feel the ans is none of the above. Any suggestion Regards, Shalini
vadiraj vd
Ranch Hand
Joined: Dec 15, 2000
Posts: 68
posted
0
Yes shalini, you're right. The compiler got confused when we do <PRE> Scope s = new Scope(); s.amethod(20); </pre> There's no way to tell the compiler that 20 in s.amethod(20) is an integer since java does automatic promotion.
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Question 23 from Marcus Green's mock exam 1
Which of the following methods can be legally inserted in place of the comment //Method Here ?
No it's correct. There's no method call involved in the test code. 2 & 3 are both valid example of overloading (across classes) and they do not cause any ambiguity by themselves. From JLS 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." An actual call to a method might cause an ambiguity error though. This happens due to (somewhat) complicated mechanism Java uses to resolve an overloaded method call by using widenening conversion for (a)parameters and (b)the class. HTH, - Manish [This message has been edited by Manish Hatwalne (edited October 24, 2001).]
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
Manish is correct. Answere 2 and 3 are valid. You added code not in the original question.
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
>No it's correct. There's no method call involved in the test >code. 2 & 3 are both valid example of overloading (across >classes) and they do not cause any ambiguity by themselves.
>An actual call to a method might cause an ambiguity error >though.
I agree with you. The code as originally written does not cause a compile error. But what is the point of overloading a method if you can never call it because it would be ambiguous? Not very practical.
Originally posted by Marilyn deQueiroz: But what is the point of overloading a method if you can never call it because it would be ambiguous? Not very practical.
Absolutely!!! But the exam has some code which is anything but prcatical (such as nested ternary operator, messing around with arithmetic operations based on the precedence, nested inner classes and so on...). If I see some of the code which is presented in the exam while doing real life code review, I would perhaps ask the person to re-write the entire code, but as far as exams are concerned, well, ......... - Manish
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Thanks, Manish
Michael Stein
Greenhorn
Joined: Nov 03, 2001
Posts: 2
posted
0
Can't you force the 10 in s.amethod(10) to be a Long by using 10L instead of 10?
Originaly posted by Michael Stein : Can't you force the 10 in s.amethod(10) to be a Long by using 10L instead of 10?
Yes, it is correct for long, but likewise if you try passing an integer value to the function, it will result in an error.
Call at 1 will give compiler error despite of passing an integer to it. Correct way of invoking is as shown at 2, which explicitly instructs the compiler which method to invoke. In fact, that is one more solid reason why code given above (as it is) is a perfectly valid code and given answers are correct. Also, have a look at this discussion. HTH, - Manish [This message has been edited by Manish Hatwalne (edited October 25, 2001).]
Nisheeth Kaushal
Ranch Hand
Joined: Jul 20, 2001
Posts: 87
posted
0
Great Going Manish.
Marcus Green
arch rival
Rancher
Joined: Sep 14, 1999
Posts: 2813
posted
0
I create my questions based on the assumption that the questions in the real exam are not too absurdly convoluted. Anyone want to comment on the obscurity/bizzareness of this question by contrast with those found on the real exam? Marcus ------------------ http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews Java 2 Exam Prep, 2nd Edition by Bill Brogden and Marcus Green ================================================= JCHQ, Almost as good as JavaRanch =================================================