This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple interface & constructor question

 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol oh dear, thanks for pointing that out!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic