| Author |
Simple interface & constructor question
|
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
Hi all,
Interfaces still get me! I have a class ClassOne...
And then I have another class, ClassTwo.
I'd like to obtain a Sequencer instance of ClassOne, but does ClassOne need to implement the entire Sequencer interface (i.e. every single method) for this to be made (http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/midi/Sequencer.html)? I hoped that by calling MidiSystem.getSequencer() in ClassOne this would be suffice for ClassTwo's code to work, but I get the error "cannot find symbol" for 'new ClassOne()'.
What ought I be doing to get the Sequencer instance of ClassOne?
Thank you for any insight on the matter!
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
but I get the error "cannot find symbol" for 'new ClassOne()'.
The problem is that the compiler does not find ClassOne in your classpath. In which package is ClassOne.java ? Did you import it in ClassTwo ? How did you compile ClassTwo.java ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
ClassOne does not implement Sequencer.
If you want to retrieve the Sequencer through ClassOne, I see two possibilities:
1) give ClassOne a Sequencer field and a getter for it:
2) Make ClassOne implement Sequencer, and delegate control to the backing Sequencer:
I'd go for solution one, mostly because it's much easier and I see no benefit in making ClassOne a Sequencer itself.
One other thing: if you always use ClassOne like that (i.e. you use it only for retrieving a Sequencer) you may consider making it a utility class with a static method:
You can then simply use ClassOne.getSequencer() to retrieve the Sequencer object.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
Thank you both.
Rob I followed your advice and it seemed to do the trick - now there's only a problem with static methods
|
 |
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
Hey guys, I have an issue with interfaces in general (didn't want to create a new topic for it).
If say I wanted to implement Sequencer after all like so:
Why do I get an error highlighting the implement line saying something like this?
"ClassOne is not abstract and does not override abstract method setLoopCount(int) in ...etc etc... Sequencer"
Cheers.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3026
|
|
Ravaa Bal wrote:Hey guys, I have an issue with interfaces in general (didn't want to create a new topic for it).
If say I wanted to implement Sequencer after all like so:
First, don't do this. Your getLoopCount() will call getLoopCount() which will call getLoopCount() which will... you get the idea. An infinite recursive loop. What do you really want getLoopCount() to return?
Why do I get an error highlighting the implement line saying something like this?
"ClassOne is not abstract and does not override abstract method setLoopCount(int) in ...etc etc... Sequencer"
Cheers.
Looks like the method defined in the interface is:
And you are implementing a method like:
These aren't the same method. What is the difference? How do you fix it?
|
Steve
|
 |
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
lol oh dear, thanks for pointing that out!
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Simple interface & constructor question
|
|
|