The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joshua Bloch
Author and "Sun God"
Ranch Hand
Joined: May 30, 2001
Posts: 124
posted
0
I do discuss this one in a fair amount of detail in Effective Java Item 16 ("Prefer interfaces to abstract classes"). Here's the summary:
An interface is generally the best way to define a type that permits multiple implementations. An exception to this rule is the case where ease of evolution is deemed more important than flexibility and power. Under these circumstances, you should use an abstract class to define the type, but only if you understand and can accept the limitations. If you export a nontrivial interface, you should strongly consider providing a skeletal implementation to go with it. Finally, you should design all of your public interfaces with the utmost care and test them thoroughly by writing multiple implementations.
Regards,
Josh
Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
M Jay
Ranch Hand
Joined: Sep 21, 2004
Posts: 66
posted
0
Steve,
Thanks for the links, very helpful. Ilja, as those guys have written a book about Java, I guessed they would have the exact correct answer for me, maybe there is something in their book that they have written regarding this, especially as their book seems to be gearded towards experienced coders.
Sathya Srinivasan
Ranch Hand
Joined: Jan 29, 2002
Posts: 379
posted
0
I am not an author, but I'll try.
If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)
If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.
Thanks for the links, very helpful. Ilja, as those guys have written a book about Java, I guessed they would have the exact correct answer for me, maybe there is something in their book that they have written regarding this, especially as their book seems to be gearded towards experienced coders.
Interfaces and Abstract classes are really Object-Oriented Programming concepts and not really specific to Java. By directing a question to a specific set of people (not that Joshua can't answer it - he has written some of the best books in Java), you are losing the perspectives of different people from different backgrounds and areas of expertise, which normally comes in handy.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Sathya Srinivasan:
Interfaces and Abstract classes are really Object-Oriented Programming concepts and not really specific to Java.
In my view, interfaces are specific to languages without multiple inheritance. In languages with multiple inheritance, interfaces are equivalent to fully abstract classes.
Or, with other words, the only difference between an interface and a fully abstract class in java is that the interface allows for multiple inheritance.
Kj Reddy
Ranch Hand
Joined: Sep 20, 2003
Posts: 1697
posted
0
Originally posted by Sathya Srinivasan: I am not an author, but I'll try.
If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)
If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.
This is very good explanation with nice example.
Kai Witte
Ranch Hand
Joined: Jul 17, 2004
Posts: 354
posted
0
hello,
Originally posted by Sathya Srinivasan:
If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)
If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.
I disagree with that, and the swing API is not designed very well. If there are common features that many implementing classes of an interface would need, then make an abstract class that implements your interface. An example for this design is java.util.List and java.util.AbstractList. With this approach clients of your API can still make their own interfaces which extend your interface. If you would only provide an abstract class that would not be possible. This is only one of many reasons why the strict "abstract class" approach is wrong.
Using an abstract class as a type (declaration, parameter or return type) violates the principle of programming to an interface.
I wrote something about that here. We also had the same discussion a couple of weeks ago in this thread.