Задайте вопросЗадайте вопрос
 

ОтвеченоWhat's the difference between Abstract classes and Interfaces?

Ответы

  • 16 февраля 2007 г. 7: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

  • 16 февраля 2007 г. 8: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.
  • 16 февраля 2007 г. 8: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.

  • 16 февраля 2007 г. 8: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

Все ответы