Re: interface vs. inheritance

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

From: Jeff Louie (jeff_louie_at_yahoo.com)
Date: 02/08/05


Date: Tue, 08 Feb 2005 13:48:48 -0800

1)Favor containment.
2)Avoid extending a class unless it was designed to be extended.
3)If you have a well defined contract that will be implemented by many
classes use an interface. An interface represents a purely abstract IS_A
relationship.
4)If the contract is evolving, consider using a base class so that you
can provide default implementations of any new methods so that clients
that use your base class can be recompiled without breaking.
5)Use a base class if you need to include implementation details.

That said... every time I try to solve a problem I seem to start by
describing the contract with an interface say IVersionable. Then I end
up writing an abstract class with default implementations as in
AbstractVersion. Then I end up creating concrete classes based on the
Abstract class as in Version : AbstractVersion.

So I would try this pattern: Interface -- AbstractInterface -- Class
which uses both interfaces and base classes and offers both options to
the user of the "interface/class."

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Relevant Pages

  • Re: Interface vs. Virtual methods
    ... If you are designing a contract without any implementation details and ... use a base class if you ... to include some implementation details. ... published an interface, it is difficult to update an interface. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: contract/interface c# system achitecture
    ... Because you only have single inheritance, using a base class means that you ... When requiring an implementation of a contract from external sources, ... would say you should always take an interface. ... replace the implementation without the client knowing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: difference between inheritance and interface?
    ... By implementing an interface, your object guarantees that the methods laid ... By extending a base class (inheritance), you inherit from the base class ...
    (comp.lang.java.programmer)
  • Re: Good example of abstract class situation
    ... abstract quicksort class with an abstract Compare ... when to use inheritance vs when to use an interface. ... If the contract is evolving, consider using a base class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: creaping coupling......
    ... but it is a more strict contract than required. ... Then its interface becomes decoupled from B's interface. ... >> efficient code the compiler should know the size of the objects in advance. ... the shop chickens need to get a method "sell", ...
    (comp.object)