• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Marcus Green - Qn : 23 - Doubt

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


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 answers given are 2 & 3.

Now my question is if I call
Scope s=new Scope();
s.amethod(10) ; //any integer

I agree. It doesn't compile:
"reference to amethod is ambiguous, both method amethod(int) in Base and method amethod(long) in Scope match"
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish is correct. Answere 2 and 3 are valid. You added code not in the original question.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Manish
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you force the 10 in s.amethod(10) to be a Long by using 10L instead of 10?
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Going Manish.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
=================================================
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic