aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading confusion" Watch "overloading confusion" New topic
Author

overloading confusion

anandkumar gangoni
Greenhorn

Joined: May 30, 2004
Posts: 16
01. public class Test {
02.
03. public long add(long x, int y) {
04. return x + y;
05. }
06.
07. public static void main(String[] args) {
08. byte x = 5;
09. int y = 6;
10. Test t = new Test();
11. t.add(5, 6);
12. }
13. }

which of the following methods can legally added to class Test?

the options:-

a.public long add(int x, long y) {return x + y;}
b.public int add(int x, int y) {return x + y;}
c.public byte add(byte x, byte y) {return x + y;}
d.public int add(int x, byte y) {return x + y;}
e.public short add(byte x, int y) {return x + y;}

the answers given is b and d.

this is given in http://www.aniyog.com/java/certification/java_mock_test.htm. But when I compiled and executed this all options were correct.

I think the answers given is worng.

plz someone help me on this confusion to get resolved....

Thanks in advance.
Wladimir Babitzki
Greenhorn

Joined: Apr 27, 2004
Posts: 19
Hi!

Have you tried to compile your example?

The compiler refuses C and E. The result of the add operation is int, however int doesn't fit to the return type.

Anyway it's not clear, why the A couldn�t be an satisfied answer.
[ June 11, 2004: Message edited by: Wladimir Babitzki ]
anandkumar gangoni
Greenhorn

Joined: May 30, 2004
Posts: 16
yeah u r correct c and e give pbm becoz of return type incompatible pbm. And is also not giving error when I compiled. I think the answer given is worng.

Regards,
Anand.
Baps Vakkalagadda
Ranch Hand

Joined: May 25, 2004
Posts: 47
It is definitely include the option A in a class. It seems the question is little bit tricky. I think it was asked in the context of the given code.

t.add(5, 6);

This is the one which makes it illegal to include option A method declaration. Because these arguments fit into both option A method declaration and the existing method decalration. So JVM complains about the ambiguity. That is the reason why it is not legal to have the option A in the given class.
Baps Vakkalagadda
Ranch Hand

Joined: May 25, 2004
Posts: 47
Correction to my first sentence. I missed a couple of words while typing (It happens many times for me).

It is definitely legal to include the option A in a class. But It seems the question is little bit tricky.
anandkumar gangoni
Greenhorn

Joined: May 30, 2004
Posts: 16
but when I executed the code it compiled & executed with out errors. I am talking abt. option a.
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
Anand

Option A will not work because it will lead to ambiguity. I have tried it. The error is -

reference to add is ambiguous, both method add(long,int) in Test3 and method add(int,long) in Test3 match

So you can very well add that method but then, you have to remove line 11. or you can cast the parameters - eg
Baps Vakkalagadda
Ranch Hand

Joined: May 25, 2004
Posts: 47
I am not sure how did you make it compile. I copied the code that Anand posted and included the option A method definition. I tried with Java 1.4.2, 1.3.1 and 1.2.2 on Windows. All of these version complained the same.

Have you tried it on Mac or Linux?
anandkumar gangoni
Greenhorn

Joined: May 30, 2004
Posts: 16
Shilpi

I compiled by including the option a and it worked well for me. I am not sure how u got that error.

but some time back I encountered a pretty similar pbm which had the same error as u said... at present I don't have that code snippet with me.

anyways thanks for ur views.
Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
I also succeed to compile the code with number "a". Besides, I found following from the book I refer-Complete Java2 Certification by Simom Roberts, Philip Heller and Michael Ernest.

--- from the book.
1. public void aMethod(String s){}
2. public void aMethod(){}
3. public void aMethod(int i, String s){}
4. public void aMethod(String s, int i) {}

"These methods all have identical return types and names, but their argument lists are different either in the types of the arguments that they take or in the order."
---

Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading confusion
 
Similar Threads
can you please explain what the return type can be possibly?
Initilization block
Chapter 1. Question 9
ID:11 SCJP Question of the Day !!!