{"id":5652,"date":"2023-12-30T09:38:21","date_gmt":"2023-12-30T08:38:21","guid":{"rendered":"https:\/\/ekiwi.de\/index.php\/5652\/c-virtual-vs-abstract-2\/"},"modified":"2023-12-30T09:54:00","modified_gmt":"2023-12-30T08:54:00","slug":"c-virtual-vs-abstract-2","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/5652\/c-virtual-vs-abstract-2\/","title":{"rendered":"C# virtual vs. abstract"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5652\/c-virtual-vs-abstract-2\/#Abstract_classes\" >Abstract classes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5652\/c-virtual-vs-abstract-2\/#Abstract_methods\" >Abstract methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5652\/c-virtual-vs-abstract-2\/#Virtual_methods\" >Virtual methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5652\/c-virtual-vs-abstract-2\/#Differences_between_abstract_and_virtual_summarized\" >Differences between abstract and virtual summarized<\/a><\/li><\/ul><\/nav><\/div>\n<p>When programming in C# on more or less large projects, sooner or later you will come across classes and methods that have a special modifier in front of them, such as the modifiers <em><strong>abstract<\/strong> <\/em>and <em><strong>virtual<\/strong><\/em>. It is not immediately obvious what the difference is between <em>abstract<\/em> and <em>virtual<\/em>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Abstract_classes\"><\/span>Abstract classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An essential feature is that there can be abstract classes that can be preceded by the modifier abstract. However, there are no virtual classes. In addition to abstract, classes can also be preceded by the modifiers <em>static<\/em>, <em>sealed<\/em> and <em>partial<\/em>.<\/p>\n<p>If you create an abstract class, you determine how it behaves in the context of inheritance. No instances can be <a href=\"https:\/\/ekiwi.de\/en\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/\" title=\"Visual Studio: Create method from selected code lines (refactoring\/redesign)\">created from<\/a> an abstract class. The abstract class therefore only defines the basic framework for the properties to be inherited by a derived class.<\/p>\n<p>For example, such a class could be called Animal, which defines the basic structure and from which all other animal classes, e.g. Bird, are then derived.<\/p>\n<figure>\n<pre><code><span style=\"color: #3366ff;\">public abstract class<\/span> <span style=\"color: #33cccc;\">Animal<\/span>\r\n{\r\n    <span style=\"color: #3366ff;\">public string<\/span>? Name { <span style=\"color: #3366ff;\">get<\/span>; <span style=\"color: #3366ff;\">set<\/span>; }\r\n    <span style=\"color: #3366ff;\">public int<\/span> Age { <span style=\"color: #3366ff;\">get<\/span>; <span style=\"color: #3366ff;\">set<\/span>; }\r\n    <span style=\"color: #3366ff;\">public abstract void<\/span> <span style=\"color: #993300;\">Eat<\/span>();\r\n    <span style=\"color: #3366ff;\">public abstract void<\/span> <span style=\"color: #993300;\">Sleep<\/span>();\r\n}<\/code><\/pre><figcaption>C# Code example abstract class<\/figcaption><\/figure>\n<figure>\n<pre><code><span style=\"color: #3366ff;\">public class<\/span> <span style=\"color: #33cccc;\">Bird<\/span>:<span style=\"color: #33cccc;\">Animal<\/span>\r\n{\r\n    <span style=\"color: #3366ff;\">public override void<\/span> <span style=\"color: #993300;\">Eat<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Debug<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Bird Eat\"<\/span>);\r\n    }\r\n\r\n    <span style=\"color: #3366ff;\">public override void<\/span> <span style=\"color: #993300;\">Sleep<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Debug<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Bird Sleep\"<\/span>);\r\n    }\r\n    <span style=\"color: #3366ff;\">public<\/span> <span style=\"color: #33cccc;\">Bird<\/span>()\r\n    {\r\n        Name = <span style=\"color: #ff0000;\">\"Bird\"<\/span>;\r\n        Age = 1;\r\n    }\r\n}<\/code><\/pre><figcaption>C# Code example derived class from abstract class<\/figcaption><\/figure>\n<p>As you can see, abstract methods are also defined within the class. To avoid compiler errors, the methods defined as abstract must also be implemented in the derived classes. This means that abstract classes are very similar to <a href=\"https:\/\/learn.microsoft.com\/de-de\/dotnet\/csharp\/language-reference\/keywords\/interface\" target=\"_blank\" rel=\"noopener\"><em>Interfaces<\/em><\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Abstract_methods\"><\/span>Abstract methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In the code example above, we saw that methods can also be defined as <em>abstract<\/em>. Such abstract methods do not contain any code and must be overwritten in the derived class with <em>override<\/em> and implemented. See code example above, where we only make a console output with <em>WriteLine<\/em> as implementation.<\/p>\n<p>The abstract methods are always part of an abstract class.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Virtual_methods\"><\/span>Virtual methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The identifier <em>virtual<\/em> is always placed in front of a method if it can be overwritten by a derived class. The big difference to <em>abstract<\/em> is that a virtual method is implemented, i.e. it already contains code. In addition, a virtual method can be used in abstract classes as well as in normal classes. We illustrate this behaviour by adding the virtual method <em>Move()<\/em> to the code example above.<\/p>\n<figure>\n<pre><code><span style=\"color: #3366ff;\">public abstract class<\/span> <span style=\"color: #33cccc;\">Animal<\/span>\r\n{\r\n    <span style=\"color: #3366ff;\">public string<\/span>? Name { <span style=\"color: #3366ff;\">get<\/span>; <span style=\"color: #3366ff;\">set<\/span>; }\r\n    <span style=\"color: #3366ff;\">public int<\/span> Age { <span style=\"color: #3366ff;\">get<\/span>; <span style=\"color: #3366ff;\">set<\/span>; }\r\n    <span style=\"color: #3366ff;\">public abstract void<\/span> <span style=\"color: #993300;\">Eat<\/span>();\r\n    <span style=\"color: #3366ff;\">public abstract void<\/span> <span style=\"color: #993300;\">Sleep<\/span>();\r\n    <span style=\"color: #3366ff;\">public virtual void<\/span> <span style=\"color: #993300;\">Move<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Console<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Animal Move\"<\/span>);\r\n    }\r\n}<\/code><\/pre><figcaption>C# Code example with virtual method<\/figcaption><\/figure>\n<figure>\n<pre><code><span style=\"color: #3366ff;\">public class<\/span> <span style=\"color: #33cccc;\">Bird<\/span>:<span style=\"color: #33cccc;\">Animal<\/span>\r\n{\r\n    <span style=\"color: #3366ff;\">public override void<\/span> <span style=\"color: #993300;\">Eat<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Debug<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Bird Eat\"<\/span>);\r\n    }\r\n\r\n    <span style=\"color: #3366ff;\">public override void<\/span> <span style=\"color: #993300;\">Sleep<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Debug<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Bird Sleep\"<\/span>);\r\n    }\r\n    <span style=\"color: #3366ff;\">public<\/span> <span style=\"color: #33cccc;\">Bird<\/span>()\r\n    {\r\n        Name = <span style=\"color: #ff0000;\">\"Bird\"<\/span>;\r\n        Age = 1;\r\n    }\r\n    <span style=\"color: #3366ff;\">public override void<\/span> <span style=\"color: #993300;\">Move<\/span>()\r\n    {\r\n        <span style=\"color: #33cccc;\">Debug<\/span>.<span style=\"color: #993300;\">WriteLine<\/span>(<span style=\"color: #ff0000;\">\"Bird is jumping\"<\/span>);\r\n    }\r\n}<\/code><\/pre><figcaption>C# Code example derived class with override virtual method<\/figcaption><\/figure>\n<p>The virtual method can be overwritten in the derived class, but does not have to be overwritten.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Differences_between_abstract_and_virtual_summarized\"><\/span>Differences between abstract and virtual summarized<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><em>abstract<\/em> can be used as a modifier for classes; <em>virtual<\/em> cannot<\/li>\n<li><em>abstract<\/em> and <em>virtual<\/em> can be prepended to methods<\/li>\n<li>abstract methods are part of abstract classes<\/li>\n<li>virtual methods can be part of abstract and normal classes<\/li>\n<li>abstract methods <strong>do not contain any code<\/strong> and <strong>must<\/strong> be <strong>overridden <\/strong>in the derived class<\/li>\n<li>virtual methods contain code and can be overridden in derived classes<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>When programming in C# on more or less large projects, sooner or later you will come across classes and methods<\/p>\n","protected":false},"author":2,"featured_media":1011,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[870],"tags":[872,989,1132,875],"class_list":["post-5652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-en","tag-code-en","tag-coding-en","tag-visual-studio-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=5652"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/1011"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=5652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=5652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=5652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}