About Question enthuware.ocpjp.v8.2.1308 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
runnerdave
Posts: 12
Joined: Mon Jan 30, 2017 2:58 pm
Contact:

About Question enthuware.ocpjp.v8.2.1308 :

Post by runnerdave »

Can you please elaborate on the explanation option 5?
class MidiPlayer implements MusicPlayer<Instrument> {
    public void play(Guitar g){ }
}

"MidiPlayer must have a method play(Object )."

Why it has to be Object and not Instrument?

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

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by admin »

Because the declaration of MusicPlayer does not apply generic variable to Player. Notice that it extends Player{ } and not extends Player<E>{ }.

Since the type of Player is not restricted in MusicPlayer, the class would have to implement play(Object ).
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by __JJ__ »

Hi
So it is just happenstance that the E in

Code: Select all

interface Player<E>{ void play(E e); 
and the E in

Code: Select all

interface MusicPlayer<E extends Instrument> extends Player{ }
are the same, yes? The E in MusicPlayer<E...> has no connection whatsoever with the E in Player<E>?

Thank you.

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

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by admin »

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

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by __JJ__ »

Well it seems one has to try the reverse case to prove it:

Code: Select all

interface Player<E>{ void play(E e); }
interface GamePlayer<Floozy extends Game> extends Player<Floozy>{ }
interface MusicPlayer<E extends Instrument> extends Player{ }

class Batsman implements GamePlayer<Cricket>{ 
    public void play(Number o){ } 
}

ocp8\practice2\T27.java:57: error: Batsman is not abstract and does not override abstract method play(Cricket) in Player
class Batsman implements GamePlayer<Cricket>{
^
1 error

class Batsman implements GamePlayer<Cricket>{ 
    public void play(Cricket o){ }  //OK
}

So the answer is yes, it is just happenstance; the name used for the generic type placeholder in GamePlayer has no relationship to that used in Player.
NB is is kind of the compiler to tell you what you should be overriding...

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by Bhaskar »

Observe that this is a non-typed usage of MusicPlayer. Since MusicPlayer has not been typed to anything, that means, it should be able to work with any object. Thus, MidiPlayer must have a method play(Object ).
Shouldn't the explanation to option four say "this is a non-typed usage of Player" instead of MusicPlayer? Because whatever the MusicPlayer is typed to, the overridden method play in MidiPlayer class can only take Object as arguments, i.e, it depends on the type of Player and not of MusicPlayer. Please have a look at the below example.

Code: Select all

class MidiPlayer implements MusicPlayer<Guitar> {     public void play(Guitar g){ } } // Compilation Error
class MidiPlayer implements MusicPlayer<Guitar> {     public void play(Object g){ } } // Works fine 
Last edited by Bhaskar on Sat Jan 11, 2020 5:51 am, edited 1 time in total.

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

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by admin »

You are right. It should be Player. MusicPlayer extends plain untyped Player. This means, MusicPlayer interface gets the abstract method play(Object obj) from Player. Thus, any non-abstract class that implements MusicPlayer must have play(Object ) method. Thus, MidiPlayer must have a method play(Object ).
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by Bhaskar »

Thanks for the explanation. I would also like to add something that i have tinkered out.

Code: Select all

class Game{ }
class Cricket extends Game{ }
class Instrument{ }
class Guitar extends Instrument{ }

interface Player<E>{ void play(E e); }
interface GamePlayer<E extends Game> extends Player<E>{ }
interface MusicPlayer<E extends Instrument> extends Player{ 
    void play(E e);
}

class Test implements MusicPlayer<Instrument>{
    @Override
    public void play(Object e) {}
    
    @Override
    public void play(Instrument e) {}    
}
Although it looks like MusicPlayer is overriding the play method of Player, but in fact it's overloading it. Therefore Test class has to provide implementations of play methods from both MusicPlayer as well as Player

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

Re: About Question enthuware.ocpjp.v8.2.1308 :

Post by admin »

Yes, that's because you have declared void play(E e) in MusicPlayer<E extends Instrument>. So, now, MusicPlayer has play(Instrument ) of its own and play(Object ) from Player.

The following is still an override:

interface MusicPlayer extends Player<Instrument>{ }
class MidiPlayer implements MusicPlayer {
public void play(Instrument g){ }
}
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 73 guests