What's the difference between Abstract classes and Interfaces?<p>What's de advantage ov Interfaces and vice versa the advantage of the Abstract Classes?</p> <p>Thanks</p>© 2009 Microsoft Corporation. All rights reserved.Fri, 04 Sep 2009 20:14:37 Z8ad621b8-a915-4d7e-89c3-5dbbc47202fdhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#8ad621b8-a915-4d7e-89c3-5dbbc47202fdhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#8ad621b8-a915-4d7e-89c3-5dbbc47202fdGaregin Gulyanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Garegin%20GulyanWhat's the difference between Abstract classes and Interfaces?<p>What's de advantage ov Interfaces and vice versa the advantage of the Abstract Classes?</p> <p>Thanks</p>Fri, 16 Feb 2007 07:17:41 Z2007-02-16T11:41:33Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#584dfa74-3e3d-41bd-8e11-79271f0a7cbehttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#584dfa74-3e3d-41bd-8e11-79271f0a7cbeGeert Verhoevenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Geert%20VerhoevenWhat's the difference between Abstract classes and Interfaces?<P><FONT face="Verdana, Geneva, Arial, Sans-serif" size=2>An abstract class is a class that can not be instantiated but that can contain code. <BR></FONT><FONT face="Verdana, Geneva, Arial, Sans-serif" size=2>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. </FONT></P> <P><FONT face=Verdana size=2>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&nbsp;an interface.</FONT></P> <P><FONT face=Verdana size=2>You can implement multiple interfaces but only inherit from one class.</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif" size=2>Greetz,</FONT></P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif" size=2>Geert</FONT></P> <P>&nbsp;</P> <P><FONT face="Verdana, Geneva, Arial, Sans-serif" color=#483d8b size=2><B>Geert Verhoeven</B><BR><FONT color=#708090>Consultant @ <a target="_blank" title="http://www.ausy.be/" href="http://www.ausy.be/"><FONT color=#708090>Ausy Belgium</FONT></a></FONT></FONT></P> <P><a target="_blank" title="http://geertverhoeven.blogspot.com/" href="http://geertverhoeven.blogspot.com/"><FONT face="Verdana, Geneva, Arial, Sans-serif" color=#008000 size=2>My Personal Blog</FONT></a></P>Fri, 16 Feb 2007 07:43:44 Z2008-02-12T09:54:32Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#f06efe69-44e9-488b-b0b7-d65c878c7b21http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#f06efe69-44e9-488b-b0b7-d65c878c7b21Ernst Kuschkehttp://social.msdn.microsoft.com/Profile/en-US/?user=Ernst%20KuschkeWhat's the difference between Abstract classes and Interfaces?A good way to distinguish between a case for the one or the other for me has always been the following:<br><br>1. Are there many classes that can be &quot;grouped together&quot; and described by one <span style="font-weight:bold;font-style:italic">noun</span>? 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 <span style="font-weight:bold">Animal</span>... you would always instantiate a certain kind of <span style="font-weight:bold">Animal</span>: an implementation of your <span style="font-weight:bold">Animal </span>base class)<br><span style="font-style:italic">Example</span>: <span style="font-weight:bold">Cat </span>and <span style="font-weight:bold">Dog</span> can both inherit from abstract class <span style="font-weight:bold">Animal</span>, and this abstract base class will implement a method <span style="font-weight:bold">void Breathe() </span>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 <span style="font-weight:bold">Fish</span>, which does not breath the same as most animals).<br><br>2. What kinds of <span style="font-weight:bold;font-style:italic">verbs </span>can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.<br><span style="font-style:italic">Example</span>: All animals can be fed, so I will create an interface called <span style="font-weight:bold">IFeedable </span>and have Animal implement that. Only <span style="font-weight:bold">Dog </span>and <span style="font-weight:bold">Horse </span>are nice enough though to implement <span style="font-weight:bold">ILikeable</span> - I will not implement this on the base class, since this does not apply to <span style="font-weight:bold">Cat</span>.<br><br>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.<br>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.<br>Fri, 16 Feb 2007 08:01:16 Z2007-02-16T11:40:20Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#5f72e6be-5aed-41e2-91fb-27b5e2ca0f40http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#5f72e6be-5aed-41e2-91fb-27b5e2ca0f40Figo Feihttp://social.msdn.microsoft.com/Profile/en-US/?user=Figo%20FeiWhat's the difference between Abstract classes and Interfaces?<P>Hi, Garegin</P> <P>An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.<BR><BR>An interface can have neither of the two.</P> <P>Since it is a frequently asked question, you can see the following answers by others:</P> <P><a target="_blank" title="http://en.csharp-online.net/Interfaces_and_Abstract_Classes" href="http://en.csharp-online.net/Interfaces_and_Abstract_Classes">http://en.csharp-online.net/Interfaces_and_Abstract_Classes</a></P><FONT face=Arial color=#013da4><a target="_blank" title="http://www.codeproject.com/csharp/abstractsvsinterfaces.asp" href="http://www.codeproject.com/csharp/abstractsvsinterfaces.asp">http://www.codeproject.com/csharp/abstractsvsinterfaces.asp</a></FONT> <P><a target="_blank" title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&amp;SiteID=1</a></P> <P>If you have further questions, pls feel free to let us know. Thank you.</P>Fri, 16 Feb 2007 08:37:30 Z2007-02-16T08:37:35Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#35dc0421-010b-4ce0-963a-8e7189deaab6http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#35dc0421-010b-4ce0-963a-8e7189deaab6Jim Tomaskohttp://social.msdn.microsoft.com/Profile/en-US/?user=Jim%20TomaskoWhat's the difference between Abstract classes and Interfaces?<p>Good question... interfaces and abstract classes have so much in common it's hard to make a desision at times.</p> <p>Here's a quote from &quot;The Complete Reference C# 2.0&quot; by Herbert Schildt that I like:</p> <p>&quot;When you can fully describe the concept in terms of &quot;what it does&quot; without needing to specify any of &quot;how it does it&quot;, 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.&quot;</p> <p>Happy designing,</p> <p>Jim Tomasko</p>Fri, 16 Feb 2007 08:39:54 Z2007-02-16T11:41:33Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#e7595018-5c10-4e19-a5f1-facf7611aa6chttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#e7595018-5c10-4e19-a5f1-facf7611aa6cGaregin Gulyanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Garegin%20GulyanWhat's the difference between Abstract classes and Interfaces?<p>Thanks for such a good example!</p> <p>Hope we'll learn many things from each other.</p>Fri, 16 Feb 2007 11:05:10 Z2007-02-16T11:05:10Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#ca13aef1-e05e-45fd-b0ae-41c3f0cd2a71http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#ca13aef1-e05e-45fd-b0ae-41c3f0cd2a71Garegin Gulyanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Garegin%20GulyanWhat's the difference between Abstract classes and Interfaces?<p>Thank You.</p>Fri, 16 Feb 2007 11:21:25 Z2007-02-16T11:21:25Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#17e9a161-81f8-4586-8093-4e3a112acde8http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#17e9a161-81f8-4586-8093-4e3a112acde8Garegin Gulyanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Garegin%20GulyanWhat's the difference between Abstract classes and Interfaces?Thanks for such good links.Fri, 16 Feb 2007 11:37:17 Z2007-02-16T11:37:17Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#9abfaa6a-73af-470b-9164-85cc16865926http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#9abfaa6a-73af-470b-9164-85cc16865926Matthew Watsonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Matthew%20WatsonWhat's the difference between Abstract classes and Interfaces?There are three fundamental difference between interfaces and abstract base classes, two of which have already been described in this thread:<br><br>1) Abstract base classes can include code (methods).<br>2) Abstract base classes can include data (fields).<br>3) You can multiply-inherit from interfaces, but you cannot multiply-inherit from abstract base classes.<br><br>Mon, 19 Feb 2007 11:07:09 Z2007-02-19T11:07:09Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#f15ad0c0-ab10-4b83-93a4-5bc3168b3a5ahttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#f15ad0c0-ab10-4b83-93a4-5bc3168b3a5aErnst Kuschkehttp://social.msdn.microsoft.com/Profile/en-US/?user=Ernst%20KuschkeWhat's the difference between Abstract classes and Interfaces?I prefer not to use the term &quot;inherit&quot; when talking about interfaces - an interface is implemented <img src="/MSDN//emoticons/emotion-1.gif" alt=Smile><br><br>An interface describes in which ways a class can be &quot;interfaced&quot; (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!) <img src="/MSDN//emoticons/emotion-5.gif" alt=Wink><br>Tue, 20 Feb 2007 14:37:39 Z2007-02-20T14:37:39Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#7e22a04e-fc10-4e65-a73f-9ae3ad3f65cfhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#7e22a04e-fc10-4e65-a73f-9ae3ad3f65cfMatthew Watsonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Matthew%20WatsonWhat's the difference between Abstract classes and Interfaces?Maybe so, but the terms tend to be used interchangably even within the official documentation:<br><br><a title="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcreftheinterfacetype.asp" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcreftheinterfacetype.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcreftheinterfacetype.asp</a><br><br>Tue, 20 Feb 2007 15:37:42 Z2007-02-20T15:37:42Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#4522e962-60d7-4ca4-8e20-12f6351d8018http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#4522e962-60d7-4ca4-8e20-12f6351d8018SeanVhttp://social.msdn.microsoft.com/Profile/en-US/?user=SeanVWhat's the difference between Abstract classes and Interfaces?<p align=left><font face=Arial size=2></font> </p>I think Ernst meant that a class that implements an interface doesn't &quot;inherit&quot; from the interface.  (Although interfaces can inherit from other interfaces, which I believe is what the link is talking about.)Tue, 31 Jul 2007 19:31:27 Z2007-07-31T19:31:27Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#d88ef5fa-7cee-4a32-b2d7-5577baee254ahttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#d88ef5fa-7cee-4a32-b2d7-5577baee254aErnst Kuschkehttp://social.msdn.microsoft.com/Profile/en-US/?user=Ernst%20KuschkeWhat's the difference between Abstract classes and Interfaces?Hey Sean, that's exactly what I meant <img alt=Wink src="http://forums.microsoft.com/MSDN/emoticons/emotion-5.gif"><br>Even though these terms are 'officially' interchangable, I prefer to differentiate between them. Just personal opinion.<br>Wed, 01 Aug 2007 08:48:53 Z2007-08-01T08:48:53Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#2def3719-8ea0-44e4-b9fc-62431f5e1fb1http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#2def3719-8ea0-44e4-b9fc-62431f5e1fb1Yakov72http://social.msdn.microsoft.com/Profile/en-US/?user=Yakov72What's the difference between Abstract classes and Interfaces?This one usually stays in a shadow. But in my opinion it's a killer.<br>I like how this guy worded it:<br><br> <a href="http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx">http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx</a><br><br> <table class=ArticleTable cellspacing=0 cellpadding=7 width=568> <tbody> <tr> <td valign=top width="33%"> <p><font style="font-size:11px">Adding functionality (Versioning)</font></p></td> <td valign=top width="33%"> <p><font style="font-size:11px">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.</font></p></td> <td valign=top width="33%"> <p><font style="font-size:11px">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.</font></p></td></tr></tbody></table>Tue, 03 Jun 2008 06:32:32 Z2008-06-03T06:32:32Zhttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#b27fcc7a-a119-4368-bf1e-3f96a0b881fahttp://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd#b27fcc7a-a119-4368-bf1e-3f96a0b881faPhani_tpkhttp://social.msdn.microsoft.com/Profile/en-US/?user=Phani_tpkWhat's the difference between Abstract classes and Interfaces?<p>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?<br><br>-Phani</p>Thu, 04 Dec 2008 13:06:19 Z2008-12-04T13:06:19Z