About Question enthuware.ocpjp.v7.2.1308 :
Moderator: admin
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1308 :
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) ?
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) ?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
Yes, it is correct because the method is actually coming from interface Player<E>{ void play(E e); }
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Jun 28, 2014 8:43 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
This is my current understanding of the problem, further dissecting the uncertainty brought up by Wisevolk (see comments):
Is that accurate?
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
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
That is correct, lunars.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
Did not understand this and also whylunars wrote:This is my current understanding of the problem, further dissecting the uncertainty brought up by Wisevolk (see comments):
Is that accurate?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 }
MidiPlayer must have a method play(Object ).
Why can't I use type Instrument here?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
Try compiling using Instrument and see what the compiler error message says. That should help.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1308 :
Actually I was trying to verify if this would work
And I didn't see that MusicPlayer was extending RAW Player!!!
What I thought was
and if this was the interface then my MidiPlayer will happily compile!
Code: Select all
class MidiPlayer implements MusicPlayer<Instrument>
{
public void play(Instrument o){ }
}
What I thought was
Code: Select all
interface MusicPlayer<E extends Instrument> extends Player<E>{ }
Who is online
Users browsing this forum: No registered users and 55 guests