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.
interface T1{
}
interface T2{
int VALUE = 10;
void m1();
}
interface T3 extends T1, T2{
public void m1();
public void m1(int x);
}
This code is fine until you try to implement the T3 & T2 interface with a concrete class, at that moment you would get compilation error. Am I correct?
"Explicit cast is not required for calling the method m1() : ( ( T2) t).m1();"
I have difficulties to read/understand : ((T2)) t).m1(); . Is first time when I see something like this. Can you please help me and "translate" this, in easy words , for a dummy like me ?
Thank you very much! I am preparing for OCA certification and having your support it means a lot for me I feel like I am not alone and you make my path more easier! God bless you!
Having ambiguous fields or methods does not cause any problem by itself but referring to such fields or methods in an ambiguous way will cause a compile time error.
From the book for the default methods:
Why is it a problem having 2 default methods in a same interface but it is not a problem having to 'regular' methods in a same interface?
Both are talking about different things. Declaring two methods with the same signature in a class/intereface is always wrong, but inheriting two methods is not a compilation error. That is just how the language designers decided to design the language.
If you like our products and services, please help us by posting your review here.
I am not sure what kind of answer/reasoning you are looking for but in my opinion, that is just the way Java designers designed it. I haven't seen any reason metioned in the JLS.
If you like our products and services, please help us by posting your review here.
Declaring two methods with the same signature in a class/intereface is always wrong
What I meant is that I don't see 2 methods with the same signature in a class/interface.
1) Example from the question bank has 2 interfaces T3 and T2 which each has a method m1() -> no 2 methods with the same signature in a class/interface.
2) Example from the book (p. 254) has 2 interfaces Readable, Writable. 1 has both method staticMethod() and defaultMethod() -> no 2 methods with the same signature in a class/interface.
1. The example in the question bank is about abstract methods (there are only declarations of method m1. No implementation.) The explanation is talking about this case. If you read the explanation completely, you will see, "...the definition to both resolves unambiguously to only one m1()". So, the ambiguity is resolved because there is only one implementation.
2. The example in the book is talking about default methods i.e. methods with implementations. Having two implementations for the same method is the problem here because there is no way to resolve the ambiguity now. That is what the book is talking about. Different situations.
I understand that the rules are not very consistent in both the situations but thats the way it is. You need to make sense of them somehow.
If you like our products and services, please help us by posting your review here.
Having ambiguous methods does not cause any problem by itself but referring to such fields or methods in an ambiguous way will cause a compile time error.