About Question enthuware.ocajp.i.v7.2.900 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

About Question enthuware.ocajp.i.v7.2.900 :

Post by gparLondon »

but referring to such fields/methods in an ambiguous way will cause a compile time error.
How can a method in interface, i.e here m1() be called in ambiguous way? Can you please provide an example. I know calling a field would as its implicitly static.

Thanks,
GPAR

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by admin »

I don't think there can be any ambiguity in resolving methods in this case, at least in Java 7. In Java 8, you can have static methods in an interface as well.
But there are cases when a method can be called ambiguously. For example:

Code: Select all

interface I1{
 public void m1(short s);
}
interface I2{
 public void m1(byte s);
}
public class TestClass implements I1, I2{
  public void m1(short s){
    System.out.println("in short "+s);
  }
  public void m1(byte i){
    System.out.println("in byte "+i);
  }
  public static void main(String args[]){
     //new TestClass().m1(1); //adding this line causes compilation failure.
  }
}

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by gparLondon »

Please correct me if I am wrong, This example doesn't make the point ambiguity related to method call of an interface where 2 interfaces have same method name, and both are implemented by a class, this is just a sample of ambiguity related to calling method with a parameter, here method call is with the argument int, which wont fit into either short or byte, this can happen even we have any superclass which has these 2 methods, or one of the method is in super and another in sub, or both the methods are in same class.

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by admin »

Yes, you are right. I already said this:
I don't think there can be any ambiguity in resolving methods in this case, at least in Java 7. In Java 8, you can have static methods in an interface as well.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by gparLondon »

Ok, Thanks for the reply.

GPAR

trtlmnky
Posts: 1
Joined: Fri Oct 23, 2015 2:23 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by trtlmnky »

I thought interfaces are "implement[ed]", but in this example interface T3 "extends" T1 and T2. How does that not make the answer E - "None of the above"?

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by admin »

Interfaces are implemented by classes. But they can also be extended by other interfaces. Option E cannot be correct because the given code compiles fine and so option 2 is correct.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by NickWoodward »

so why is "There is nothing wrong with the code" not the correct answer?

doesn't 'something being wrong' indicate that the code won't compile or work, rather than it not following best practice (marking methods public in an interface), or unnecessary method declarations in T3.

or have I missed something? :D

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by admin »

That is indeed the correct answer.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by NickWoodward »

whoops! my mistake, i must have been tired!

phusztek
Posts: 6
Joined: Tue Nov 22, 2016 11:04 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by phusztek »

T3.m1() is also fine because even though m1() is declared in T2 as well as T3 , the definition to both resolves unambiguously to only one m1(). Explicit cast is not required for calling the method m1() : ( ( T2) t).m1();
T3.m1() cannot be fine since m1() is not a static method so it cannot be called in staitic way. Or I don't see the point here?

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.900 :

Post by admin »

You are right the explanation is incorrect. It should say, "Having m1() in T3 is also fine...", which is what it is actually trying to get at.

thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 31 guests