Page 1 of 1

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

Posted: Fri May 04, 2018 1:27 pm
by flex567
What, if anything, is wrong with the following code?

Code: Select all

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?

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

Posted: Fri May 04, 2018 8:28 pm
by admin
What happened when you tried to compile it?

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

Posted: Sat May 05, 2018 2:25 am
by flex567
It compiles. Hmm this is because methods in an interface are implicitly public(so the method in T2 is public).

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

Posted: Sat May 12, 2018 12:35 pm
by mihhay
"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!

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

Posted: Sat May 12, 2018 8:43 pm
by admin
You should work out the expression ( ( T2) t).m1(); according to brackets just like you execute a mathematical equation ( (2+3) x 4).

So first you have to see what ( ( T2) t) means. It means t is being cast as type T2, and then a method m1 is invoked on t.

If you are seeing this the first time, then I sincerely suggest you to read the topic of casting from a good book first. Here are links to a couple of online material that you might want to go through:
https://stackoverflow.com/questions/530 ... ts-in-java
http://www.codejava.net/java-core/the-j ... ng-in-java

You may google more about casting to see more such articles.

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

Posted: Sat Feb 16, 2019 6:18 pm
by flex567
From the explanation:

Code: Select all

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:
Image

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?

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

Posted: Sat Feb 16, 2019 7:45 pm
by admin
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.

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

Posted: Sun Feb 17, 2019 12:27 pm
by flex567
Declaring two methods with the same signature in a class/interface is always wrong
But there arent any 2 methods declared in the same class/interface?

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

Posted: Sun Feb 17, 2019 2:05 pm
by admin
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.

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

Posted: Sun Feb 17, 2019 5:20 pm
by flex567
You said:
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.

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

Posted: Sun Feb 17, 2019 9:27 pm
by admin
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.

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

Posted: Fri Feb 22, 2019 2:12 pm
by flex567
Ok but this from the explanation is not true for defualt methods:

Code: Select all

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.

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

Posted: Fri Feb 22, 2019 8:35 pm
by admin
Yes, and the question is not about default methods either.
We will update the explanation to make this point clear.

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

Posted: Wed Jul 10, 2019 6:13 am
by SuperTyp
there is a grammatical error in answer d) "The code will work fine only if m1() is removed from either T2 and T3."
/s/and/or

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

Posted: Wed Jul 10, 2019 9:08 am
by admin
SuperTyp wrote:
Wed Jul 10, 2019 6:13 am
there is a grammatical error in answer d) "The code will work fine only if m1() is removed from either T2 and T3."
/s/and/or
Updated. Thank you for your feedback!