wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Need help in understanding this abstract/interface design example! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in understanding this abstract/interface design example!" Watch "Need help in understanding this abstract/interface design example!" New topic
Author

Need help in understanding this abstract/interface design example!

chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Hi to all.
I am designing a music application. I am still not clear on some issues concerning interfaces and abstract classes. If anyone can help me understand, that would be great. Here is what I came up with:

Here is the abstract class:

The questions are as follows..
1) What if I don't define List getMiscChoices() as abstract in the abstract class as opposing to doing so? What can/would change in the implementation?
2) What if I do not define
public int getMusicChoiceRef();
public String getMusicChoiceNameDisplayed(); in the interface but just the last one, ie List getMiscChoices() as opposed to this design?
3) Can anyone see any optimizations I can make?
Thank you all for your help.
-- CHAD
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
First, I think that you will want to declare AbstractMusicChoices to implement MusicChoices or there is really no point in having the MusicChoices interface:

1) What if I don't define List getMiscChoices() as abstract in the abstract class as opposing to doing so? What can/would change in the implementation?
Then you would still have an abstract class but with a default implemetation for getMiscChoices().
You still couldn't instantiate an AbstractMusicChoices object, but you could create a concrete class as simply as:

For all intents and purposes, that would be an AbstractMusicChoices object.
2) What if I do not define
public int getMusicChoiceRef();
public String getMusicChoiceNameDisplayed(); in the interface but just the last one, ie List getMiscChoices() as opposed to this design?

Well, that's just a design decision you have to make. Do you always want or need to reference these methods via the interface or not? If you are going to have many different implementations and the methods you mentioned need to be accessed, then as a general principle, you probably should leave them in the interface and always reference their instances thru the interface.
3) Can anyone see any optimizations I can make?
Well, not knowing your exact intent, that's hard to say. But it looks OK to me.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
chad stevens
Ranch Hand

Joined: Aug 20, 2002
Posts: 88
Hello Michael.
Sorry I did forget to mention the 'implements' part, I typed it up quickly, but yes, that was my intention.
Thanks for your response, you have clarified my thoughts, I was basically asking that question so that I can decided whether or not to put the get methods in the interface or not.
Thanks!
-- CHAD
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If the interface and the abstract class both contain the exact same set of methods, then there is no need for both of them. I guess I am assuming you have shown the whole class, though, so if there are additional methods in this abstract class, then a separate interface is okay. Another reason for separating out the interface is to be able to reuse it in another class hierarchy that already inherits from some other class (if that makes sense). Many of these design decisions are dependent on a few other issues that aren't in your original post.


Java API Documentation
The Java Tutorial
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

If the interface and the abstract class both contain the exact same set of methods, then there is no need for both of them.

That's not necessarily true. Consider for example javax.swing.table.AbstractTableModel which implemets javax.swing.table.TableModel. For most custom TableModel implementations, it serves as an excellant starting point, you only have to implement 3 of the 9 methods in your concrete class. Abstract classes serve a different purpose than an interface. An abstract class provides a base behavior and state, whereas an interface provide a means of communication or protocol. With two abstract classes implementing the same interface, I can create two hierarchies of vastly different behavior while fulfilling the contract imposed by the interface. Furthermore, programming to the interface usually makes a software system more flexible and extensible. I am free to completely change all my implementing classes without breaking the rest of the system.
 
Don't get me started about those stupid light bulbs.
 
subject: Need help in understanding this abstract/interface design example!