Page 1 of 1
About Question enthuware.ocajp.i.v7.2.900 :
Posted: Thu Jan 22, 2015 1:26 am
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
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Thu Jan 22, 2015 2:22 am
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.
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Thu Jan 22, 2015 8:58 am
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.
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Thu Jan 22, 2015 10:51 am
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.
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Thu Jan 22, 2015 11:14 am
by gparLondon
Ok, Thanks for the reply.
GPAR
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Fri Oct 23, 2015 2:32 pm
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"?
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Fri Oct 23, 2015 8:52 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Mon Nov 30, 2015 9:47 am
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?

Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Mon Nov 30, 2015 8:07 pm
by admin
That is indeed the correct answer.
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Fri Dec 04, 2015 12:21 pm
by NickWoodward
whoops! my mistake, i must have been tired!
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Tue Nov 29, 2016 9:22 am
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?
Re: About Question enthuware.ocajp.i.v7.2.900 :
Posted: Tue Nov 29, 2016 10:05 am
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.