About Question enthuware.ocajp.i.v8.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
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

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

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

Post by admin »

What happened when you tried to compile it?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

It compiles. Hmm this is because methods in an interface are implicitly public(so the method in T2 is public).

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

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

Post 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!

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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.

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

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

Post by admin »

Yes, and the question is not about default methods either.
We will update the explanation to make this point clear.
If you like our products and services, please help us by posting your review here.

SuperTyp
Posts: 1
Joined: Wed Jul 10, 2019 6:07 am
Contact:

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

Post 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

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

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

Post 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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests