MSDN > 論壇首頁 > Visual C# Language > What's the difference between Abstract classes and Interfaces?
發問發問
 

已答覆What's the difference between Abstract classes and Interfaces?

解答

  • 2007年2月16日 上午 07:43Geert Verhoeven 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    An abstract class is a class that can not be instantiated but that can contain code.
    An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.

    If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

    You can implement multiple interfaces but only inherit from one class.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

  • 2007年2月16日 上午 08:01Ernst Kuschke 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    A good way to distinguish between a case for the one or the other for me has always been the following:

    1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
    Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

    2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
    Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

    As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
    By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.
  • 2007年2月16日 上午 08:37Figo FeiMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    Hi, Garegin

    An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.

    An interface can have neither of the two.

    Since it is a frequently asked question, you can see the following answers by others:

    http://en.csharp-online.net/Interfaces_and_Abstract_Classes

    http://www.codeproject.com/csharp/abstractsvsinterfaces.asp

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

    If you have further questions, pls feel free to let us know. Thank you.

  • 2007年2月16日 上午 08:39Jim Tomasko 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    Good question... interfaces and abstract classes have so much in common it's hard to make a desision at times.

    Here's a quote from "The Complete Reference C# 2.0" by Herbert Schildt that I like:

    "When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface.  If you need to include some implementation details, then you will need to represent your concept in an abstract class."

    Happy designing,

    Jim Tomasko

所有回覆

  • 2007年2月16日 上午 07:43Geert Verhoeven 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    An abstract class is a class that can not be instantiated but that can contain code.
    An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.

    If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

    You can implement multiple interfaces but only inherit from one class.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

  • 2007年2月16日 上午 08:01Ernst Kuschke 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    A good way to distinguish between a case for the one or the other for me has always been the following:

    1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
    Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

    2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
    Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

    As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
    By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.
  • 2007年2月16日 上午 08:37Figo FeiMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    Hi, Garegin

    An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.

    An interface can have neither of the two.

    Since it is a frequently asked question, you can see the following answers by others:

    http://en.csharp-online.net/Interfaces_and_Abstract_Classes

    http://www.codeproject.com/csharp/abstractsvsinterfaces.asp

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

    If you have further questions, pls feel free to let us know. Thank you.

  • 2007年2月16日 上午 08:39Jim Tomasko 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    Good question... interfaces and abstract classes have so much in common it's hard to make a desision at times.

    Here's a quote from "The Complete Reference C# 2.0" by Herbert Schildt that I like:

    "When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface.  If you need to include some implementation details, then you will need to represent your concept in an abstract class."

    Happy designing,

    Jim Tomasko

  • 2007年2月16日 上午 11:05Garegin Gulyan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    Thanks for such a good example!

    Hope we'll learn many things from each other.

  • 2007年2月16日 上午 11:21Garegin Gulyan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    Thank You.

  • 2007年2月16日 上午 11:37Garegin Gulyan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Thanks for such good links.
  • 2007年2月19日 上午 11:07Matthew Watson 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    There are three fundamental difference between interfaces and abstract base classes, two of which have already been described in this thread:

    1) Abstract base classes can include code (methods).
    2) Abstract base classes can include data (fields).
    3) You can multiply-inherit from interfaces, but you cannot multiply-inherit from abstract base classes.

  • 2007年2月20日 下午 02:37Ernst Kuschke 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    I prefer not to use the term "inherit" when talking about interfaces - an interface is implemented Smile

    An interface describes in which ways a class can be "interfaced" (or interacted) with at runtime;  you can interact with the same object in multiple ways. When my class implements both IEnumerable and IDisposable, it does not inherit from multiple supers, but I can interact with this object in the ways described by the interfaces it implements (without knowledge of the actual type of the class!) Wink
  • 2007年2月20日 下午 03:37Matthew Watson 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Maybe so, but the terms tend to be used interchangably even within the official documentation:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcreftheinterfacetype.asp

  • 2007年7月31日 下午 07:31SeanV 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

     

    I think Ernst meant that a class that implements an interface doesn't "inherit" from the interface.  (Although interfaces can inherit from other interfaces, which I believe is what the link is talking about.)
  • 2007年8月1日 上午 08:48Ernst Kuschke 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hey Sean, that's exactly what I meant Wink
    Even though these terms are 'officially' interchangable, I prefer to differentiate between them. Just personal opinion.
  • 2008年6月3日 上午 06:32Yakov72 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    This one usually stays in a shadow. But in my opinion it's a killer.
    I like how this guy worded it:

     http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

    Adding functionality (Versioning)

    If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

    If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

  • 2008年12月4日 下午 01:06Phani_tpk 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    I think we can certainly say that an abstract base class with no implementation or variable definitions are as good as interfaces. isn't it?

    -Phani