Re: Abstract class or interface?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



> what are some conceptual reasons to use one over the other?

One way to look at it is "breadth" vs "depth".

You don't know much about the objects that implement your interface,
and this gives you a lot of implementation flexibility. With an
interface, you don't care what else the object can do, so long as it
implements your interface. To use a hackneyed example, imagine an

interface IText { string Text { get ; set ; } }

- you don't care if an IText reference is a desktop Form or other
Control, a web control, or a custom class. An IText[] array could
freely mix all three - every IText has a Text property, regardless of
how it's implemented or what else the object can do.

Conversely, with an abstract class, you can know things about your
objects that you can't know about an interface. (For example, you
can't mark an interface method as [Obsolete], nor is there any way to
insist that all interface implementations be [Serializable].) You can
have constructors, protected members, fields, attributes. Descendants'
implementation of various methods can rely on non-public features of
the base class.

An interface is appropriate when you don't care about anything besides
what can be expressed in an interface. Or when you need to treat a
wide variety of objects in a consistent manner. An abstract base class
is appropriate when you have a tightly coupled family of objects, that
need (or can benefit from) access to protected fields and helper
methods.

[At the risk of confusing the issue, a set of classes that implement
an interface might all descend from a common base class (besides
System.Object, that is) that provides some standard implementations of
all or part of the interface.]

As a rule of thumb, use an interface unless there's a really obvious
reason to use an abstract base class. Information Hiding Is Your
Friend.

--

www.midnightbeach.com
.



Relevant Pages

  • Re: passing a Factory to a method to create a generic instance
    ... FUNCTION FROM void TO Widget or whatever it'd be called, and then pass in a Widget class's constructor. ... Even if not, if the main thing that a Bolt is is a kind of Widget, i'd say use an abstract base class, not an interface, since it communicates a stronger sense of is-a. ...
    (comp.lang.java.programmer)
  • Re: Unification of Methods and Functions
    ... I am suggesting that we write factory methods using classmethod to give ... my classes all implement an interface which I'll call 'shape ... Rectangle and Ellipse have a common base class, ...
    (comp.lang.python)
  • Re: Self-containment in OO
    ... | class should not be dependent on its users. ... not depend on the containing class, only on its base class. ... interface of a class with things that users of that class are not able to use or are not interested in using." ... The heuristics alone as a bald list do seem a bit rough, but you have to remember there is an almost 400 page book that goes along with them. ...
    (comp.object)
  • 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 "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Alternative to Interface?
    ... >> this method to the interface and all involved classes. ... > implementation provided by the base class itself. ... > declare it as Overridable instead of MustOverride, ... that functionality will instead be placed into an existing method. ...
    (microsoft.public.dotnet.languages.vb)