About Question enthuware.ocpjp.v8.2.1308 :
Moderator: admin
-
- Posts: 12
- Joined: Mon Jan 30, 2017 2:58 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1308 :
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
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 ).
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.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
Hi
So it is just happenstance that the E in
and the E in
are the same, yes? The E in MusicPlayer<E...> has no connection whatsoever with the E in Player<E>?
Thank you.
So it is just happenstance that the E in
Code: Select all
interface Player<E>{ void play(E e);
Code: Select all
interface MusicPlayer<E extends Instrument> extends Player{ }
Thank you.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
Well it seems one has to try the reverse case to prove it:
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...
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
}
NB is is kind of the compiler to tell you what you should be overriding...
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
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.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 ).
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
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.
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
Thanks for the explanation. I would also like to add something that i have tinkered out.
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
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) {}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1308 :
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){ }
}
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.
Who is online
Users browsing this forum: No registered users and 73 guests