Page 1 of 1
About Question enthuware.ocpjp.v7.2.1308 :
Posted: Mon Sep 23, 2013 6:06 am
by Wisevolk
Hi,
class MidiPlayer implements MusicPlayer<Instrument> { public void play(Guitar g){ } }
Your answer : MidiPlayer must have a method play(Object ).
Is it right ? Shouldn't Midi player have a a method play(Instruments) ?
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Mon Sep 23, 2013 7:36 am
by admin
Yes, it is correct because the method is actually coming from interface Player<E>{ void play(E e); }
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Sat Jun 28, 2014 9:13 pm
by lunars
This is my current understanding of the problem, further dissecting the uncertainty brought up by Wisevolk (see comments):
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{ }
/**
* (1) When MusicPlayer extends the *RAW* "Player" above, that makes
* "void play(E e);" effectively convert into "void play(Object o);"
* (2) So anything implementing MusicPlayer (regardless of generics used)
* gets handed a version of Player's method(s) where E (only the E
* inside Player) has been downgraded to Object (due to being raw)
*/
class MidiPlayer implements MusicPlayer<Instrument> {
public void play(Instrument i){ } // <--
public void play(Guitar g){ } // <-- neither of these can reference the raw version of Player
public void play(Object e) {} // <-- comes from raw version of Player
}
Is that accurate?
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Sat Jun 28, 2014 10:10 pm
by admin
That is correct, lunars.
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Mon Jan 09, 2017 10:17 am
by jagoneye
lunars wrote:This is my current understanding of the problem, further dissecting the uncertainty brought up by Wisevolk (see comments):
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{ }
/**
* (1) When MusicPlayer extends the *RAW* "Player" above, that makes
* "void play(E e);" effectively convert into "void play(Object o);"
* (2) So anything implementing MusicPlayer (regardless of generics used)
* gets handed a version of Player's method(s) where E (only the E
* inside Player) has been downgraded to Object (due to being raw)
*/
class MidiPlayer implements MusicPlayer<Instrument> {
public void play(Instrument i){ } // <--
public void play(Guitar g){ } // <-- neither of these can reference the raw version of Player
public void play(Object e) {} // <-- comes from raw version of Player
}
Is that accurate?
Did not understand this and also why
MidiPlayer must have a method play(Object ).
Why can't I use type Instrument here?
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Mon Jan 09, 2017 12:17 pm
by admin
Try compiling using Instrument and see what the compiler error message says. That should help.
Re: About Question enthuware.ocpjp.v7.2.1308 :
Posted: Sat Jan 14, 2017 1:17 pm
by jagoneye
Actually I was trying to verify if this would work
Code: Select all
class MidiPlayer implements MusicPlayer<Instrument>
{
public void play(Instrument o){ }
}
And I didn't see that MusicPlayer was extending RAW Player!!!
What I thought was
Code: Select all
interface MusicPlayer<E extends Instrument> extends Player<E>{ }
and if this was the interface then my MidiPlayer will happily compile!