{"id":109,"date":"2007-10-18T11:29:00","date_gmt":"2007-10-18T11:29:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2007\/10\/18\/abstract-versus-interface\/"},"modified":"2007-10-18T11:29:00","modified_gmt":"2007-10-18T11:29:00","slug":"abstract-versus-interface","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2007\/10\/18\/abstract-versus-interface\/","title":{"rendered":"Abstract versus Interface"},"content":{"rendered":"<div>\n<p><span style=\"font-family:'Arial','sans-serif';color:black;\">An article on the <a href=\"http:\/\/msdn.microsoft.com\/msdnmag\/issues\/03\/12\/CQA\/\">MSDN<\/a> describes a summary of both the definitions. Although the article is a bit old (2003) and it compares C# with C++ it still provides a good start.<?xml:namespace prefix = o \/><o:p><\/o:p><\/span><\/p>\n<p><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">On the <a href=\"http:\/\/www.codeproject.com\/csharp\/abstractsvsinterfaces.asp\">Code Project<\/a> I found a nice article written by Rahman Mahmoodi where her has a nice comparisson for these two definitions. Another article on the <a href=\"http:\/\/www.codeproject.com\/csharp\/JmAbstractClasses.asp\">Code Project<\/a> which seems even more in dept about abstract class is written by Jayababu.<o:p><\/o:p><\/span><\/p>\n<p><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><\/p>\n<h3>Some important points about Abstract classes<\/h3>\n<p><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">In contrary to other object oriented languages like JAVA, you can not inherit from more than one abstract class<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">you can not instanciate them or you get a Compiler Error<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">An abstract class may have a non-abstract constructor implementation.<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">they might have (also) non-abstract methods having implementation. these non-abstract methods are callable from the<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:blue;\">abstract<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">class<\/span><span style=\"font-family:'Courier New';color:black;\"> AbstractClass<br \/>{<br \/><\/span><span style=\"font-family:'Courier New';color:blue;\">public<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">void<\/span><span style=\"font-family:'Courier New';color:black;\"> NonAbstractMethod()<br \/>{<br \/>Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">You can call a non-abstract method which is implemented within the abstract method.<\/span><span style=\"font-family:'Courier New';color:black;\">&#8220;);<br \/>}<br \/>}<\/p>\n<p><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">You can reimplement the non-abstract method but it would be called by the class internally or any object of that type. If you define an object of abstract type and you instanciate the derived class you still call the implementation within the abstract class. To get the reimplementation you need to instanciate the class using a reference of type of the derived class. For example defining the following in the derived class:<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:blue;\">public<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">new<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">void<\/span><span style=\"font-family:'Courier New';color:black;\"> NonAbstractMethod()<br \/>{<br \/>Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">This is new implementation of the non-abstract method.<\/span><span style=\"font-family:'Courier New';color:black;\">&#8220;);<br \/>}<\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><\/p>\n<p>and calling it like this<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:green;\">\/\/ Will call the original implementation<br \/><\/span><span style=\"font-family:'Courier New';color:black;\">AbstractClass obj = <\/span><span style=\"font-family:'Courier New';color:blue;\">new<\/span><span style=\"font-family:'Courier New';color:black;\"> InheritedClass();<br \/>obj.NonAbstractMethod();<br \/><\/span><span style=\"font-family:'Courier New';color:green;\"><br \/>\/\/ Will call the re-implementation<br \/><\/span><span style=\"font-family:'Courier New';color:black;\">InheritedClass obj2 = <\/span><span style=\"font-family:'Courier New';color:blue;\">new<\/span><span style=\"font-family:'Courier New';color:black;\"> InheritedClass();<br \/>obj2.NonAbstractMethod();<br \/><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><\/p>\n<p><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">In your derrived class you need to ovreride all the abstract methods.<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:blue;\">public<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">override<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">void<\/span><span style=\"font-family:'Courier New';color:black;\"> OverrideThis()<br \/>{<br \/>Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">You have to implement an abstract method using the override key<\/span><span style=\"font-family:'Courier New';color:black;\">&#8220;);<br \/>}<\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><\/p>\n<p><o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">An abstract method can not be marked as static, compile error.<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">You can have static and non-static fields in an abstract class:<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:blue;\">public<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">static<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">int<\/span><span style=\"font-family:'Courier New';color:black;\"> count = <\/span><span style=\"font-family:'Courier New';color:red;\">1<\/span><span style=\"font-family:'Courier New';color:black;\">;<br \/><\/span><span style=\"font-family:'Courier New';color:blue;\">public<\/span><span style=\"font-family:'Courier New';color:black;\"> <\/span><span style=\"font-family:'Courier New';color:blue;\">int<\/span><span style=\"font-family:'Courier New';color:black;\"> InstCount = <\/span><span style=\"font-family:'Courier New';color:red;\">1<\/span><span style=\"font-family:'Courier New';color:black;\">;<br \/><\/span><span style=\"font-family:'Courier New';color:blue;\"><br \/>public<\/span><span style=\"font-family:'Courier New';color:black;\"> AbstractClass()<br \/>{<br \/>count++;<br \/>InstCount++;<br \/>}<\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><br \/>but when instanciating two objects of the derived class and verify the value of the fields:<\/p>\n<p><\/span><span style=\"font-family:'Courier New';color:black;\">Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">Static field is <\/span><span style=\"font-family:'Courier New';color:black;\">&#8221; + AbstractClass.count);<br \/>Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">Instance counter is <\/span><span style=\"font-family:'Courier New';color:black;\">&#8221; + obj1.InstCount);<br \/>Console.WriteLine(&#8220;<\/span><span style=\"font-family:'Courier New';color:teal;\">Instance counter is <\/span><span style=\"font-family:'Courier New';color:black;\">&#8221; + obj2.InstCount);<\/p>\n<p>output:<\/p>\n<p>Static field is 3<br \/>Instance counter is 2<br \/>Instance counter is 2<\/p>\n<p><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><\/p>\n<p><span style=\"font-family:'Arial','sans-serif';color:black;\"><o:p><\/o:p><\/span><\/p>\n<p><h3>The Differences<\/h3>\n<\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l1 level1 lfo2\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">An inheritted class cannot have multiple base classes: compiler error.<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l1 level1 lfo2\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">An abstract class can define a default implementation (using a non-abstract method) and that will not force the derived classes to reimplement it whereas an interface can not have any implementation at all. <o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l1 level1 lfo2\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">Interfaces can not contain fields; compile error.<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l1 level1 lfo2\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\">An interface is like a contract. Once the contract changes all the classes implementing it need to change whereas an abstract class is like a common code for all the classeds and you might introduce the new method as a non-abstract method and give a default implementation. This could be seen as a pro or con. In some situations it is required to force all the contract holders to apply on the new method and in other cases you want to have this new feature for some of the types while the rest migh follow later. (a good question would be why is the new method part of the abstract class and not introduced as a new contract)<o:p><\/o:p><\/span><\/p>\n<p style=\"TEXT-INDENT: -18pt; mso-list: l1 level1 lfo2\"><span style=\"font-family:Symbol;color:black;\"><span style=\"mso-list: Ignore\">\u00b7<span style=\"FONT: 100% 'Times New Roman'\"> <\/span><\/span><\/span><span dir=\"ltr\"><\/span><span style=\"font-family:'Arial','sans-serif';color:black;\"><\/p>\n<p><o:p><\/o:p><\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>An article on the MSDN describes a summary of both the definitions. Although the article is a bit old (2003) and it compares C# with C++ it still provides a good start. On the Code Project I found a nice article written by Rahman Mahmoodi where her has a nice comparisson for these two definitions. &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2007\/10\/18\/abstract-versus-interface\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Abstract versus Interface&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[89,43,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":1,"uagb_excerpt":"An article on the MSDN describes a summary of both the definitions. Although the article is a bit old (2003) and it compares C# with C++ it still provides a good start. On the Code Project I found a nice article written by Rahman Mahmoodi where her has a nice comparisson for these two definitions.&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}