Re: Abstract class or interface?



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



.



Relevant Pages

  • Re: Abstract class or interface?
    ... >derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ... and rotate "things" along all three X, Y, and Z coordinate axis. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract Base Classes vs Interfaces?
    ... an abstract class is the only choice. ... You can take any class and derive from it adding in an interface ... > doesn't have to be derived from any particular base class. ... I think ICollection is a good example of the ambiguity between ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Abstract class or interface?
    ... > I'm still trying to figure out concrete reasons to use one over the other. ... > in its methods and derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Alternative to Interface?
    ... > this method to the interface and all involved classes. ... I declared my base class as MustInherit, and into it I put all of the ... derived classes, so I declared these as MustOverride. ... declare it as Overridable instead of MustOverride, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Abstract class or interface?
    ... >>derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ... > and rotate "things" along all three X, Y, and Z coordinate axis. ...
    (microsoft.public.dotnet.languages.csharp)

Loading