twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Difference between abstract class and interface in the desingn perspect
|
Posted: Jun 11, 2004 8:11 AM
|
|
I don't know what the "regular" answers are, but here is an explanation that makes some sense to me.
Let's start with abstract classes. If you look at the Component class you will see that it is abstract. All of its descedants share certain attributes, such as the ability to be visible on the screen, having a background and foreground color, having a size and a position. They inherit these from their common parent the Component class.
On the other hand, when you look at an interface, such as the Comparable interface, you see that it is used with many different and disparate classes. For example, the String, Integer, and Date classes all implement the Comparable interface. Clearly, none of these classes are related to one another. But because they all have the methods required by the Comparable interface, they can be used as Comparable objects as well as objects of their native class. (Keep in mind, that String's cannot be compared to Integers or Dates, only other Strings.)
Think of the descendents of an abstract class being related by blood while implementers of an interface are related by marriage. If Bob Smith marries Tina Jones, and they go to the Smith family reunion, Bob will be genetically similar to other Smiths because his is a blood connection. Tina is now a Smith, but she doesn't share any genetic link because her connection is through marriage.
When you design, ask yourself if the connection can apply to disparate classes. If it can, use an interface. But if the connection is more innate, then use an abstract class.
tom
|
|