Re: Abstract class or interface?
- From: Joshua Flanagan <josh@xxxxxxxxxx>
- Date: Sun, 01 May 2005 21:46:10 -0500
I try to make the decision based on the relationship of the derived class to the base class.
If the derived class "is a" type of the base class, I'd lean towards an abstract class. Ex: A FileStream is a type of Stream.
If the derived class "can act like" (or, has the behavior of) the type of the base class, I'd lean towards an interface. Ex: A String has the behavior of an ICloneable object. But you probably wouldn't consider the fact that it implements ICloneable as a defining characteristic of what the String class is all about; it is just one of the String class's behaviors.
Note my unwillingness to make definite statements. That's on purpose, as I consider these just guidelines, not strict rules.
Joshua Flanagan http://flimflan.com/blog
Brett wrote:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation.
Besides definitions of the two, what are some conceptual reasons to use one over the other? Perhaps examples of this project uses an abstract classes vs. this one uses an interface...for these reasons.
I have one project that uses an interface. I chose the interface over an abstract class because the derived classes all do the same thing, they just go about doing it in slightly different ways. Their results are different but conceptually what they do is the same thing. I don't ever see the need for adding more methods or properties to the interface. If there is every such a need, I can encapsulate this variability into one of the derived classes, since it will be "one" that has such a need rather than all. That make sense?
Thanks,
Brett
.
- References:
- Abstract class or interface?
- From: Brett
- Abstract class or interface?
- Prev by Date: Re: Threading
- Next by Date: Circle is not round
- Previous by thread: Re: Abstract class or interface?
- Next by thread: Re: Abstract class or interface?
- Index(es):
Relevant Pages
|
Loading