Page 1 of 1
About Question enthuware.ocpjp.v8.2.1308 :
Posted: Thu Mar 02, 2017 3:21 pm
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?
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Thu Mar 02, 2017 9:43 pm
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 ).
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jul 28, 2018 7:44 pm
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.
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jul 28, 2018 11:25 pm
by admin
What happened when you tried it out?
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sun Jul 29, 2018 6:28 am
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...
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jan 11, 2020 1:55 am
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
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jan 11, 2020 5:46 am
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 ).
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jan 11, 2020 6:19 am
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
Re: About Question enthuware.ocpjp.v8.2.1308 :
Posted: Sat Jan 11, 2020 6:33 am
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){ }
}