{"id":1933,"date":"2011-04-07T23:31:41","date_gmt":"2011-04-07T18:01:41","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1933"},"modified":"2011-04-07T23:31:41","modified_gmt":"2011-04-07T18:01:41","slug":"virtual-override-and-new-keyword-in-c","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/virtual-override-and-new-keyword-in-c\/","title":{"rendered":"Virtual, Override and new Keyword in C#"},"content":{"rendered":"<p>Consider following class Hierarchy:<\/p>\n<figure id=\"attachment_1934\" aria-describedby=\"caption-attachment-1934\" style=\"width: 114px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Multilevel-Inheritance.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1934\" title=\"Multilevel Inheritance\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Multilevel-Inheritance.jpg?resize=114%2C354&#038;ssl=1\" alt=\"Multilevel Inheritance\" width=\"114\" height=\"354\" \/><\/a><figcaption id=\"caption-attachment-1934\" class=\"wp-caption-text\">Multilevel Inheritance<\/figcaption><\/figure>\n<p>In this example, we will consider three classes which are TestA, TestB and TestC.<!--more--><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace OOPS_Concept\n{\n    class TestA\n    {\n        public void display() { Console.WriteLine(&quot;TestA - display()&quot;); }\n    }\n\n    class TestB : TestA\n    {\n        public void display() { Console.WriteLine(&quot;TestB - display()&quot;); }\n    }\n\n    class Test\n    {\n        static void Main(string&#x5B;] args)\n        {\n            TestA a;\n            TestB b;\n\n            a = new TestA();\n            b = new TestB();\n            a.display();  \/\/ TestA - display()\n            b.display();  \/\/ TestB - display()\n\n            a = new TestB();\n            a.display();  \/\/ TestA - display()\n\n            Console.Read();\n        }\n    }\n}\n<\/pre>\n<p>output :<\/p>\n<blockquote><p>TestA &#8211; display()<br \/>\nTestB &#8211; display()<br \/>\nTestA &#8211; display()<\/p><\/blockquote>\n<p>Program will compile and run successfully but<\/p>\n<ul>\n<li>The Problem of above code is that, third output should be &#8220;TestB \u2013display()&#8221;\u009d because the variable a have the object of B.<\/li>\n<li>&#8230;Test.cs(15,21): warning CS0108: &#8216;OOPS_Concept.TestB.display()&#8217; hides inherited member &#8216;OOPS_Concept.TestA.display()&#8217;. Use the new keyword if hiding was intended.<\/li>\n<\/ul>\n<p>Lets resolve the problem 1.<\/p>\n<hr \/>\n<p><strong>Keyword Virtual and Override<\/strong><br \/>\nIn C#, if you like to override the parent class method then you must mark the parent method by keyword &#8220;<strong>Virtual<\/strong>&#8220;\u009d and method in derived class which intended to override the parent method should be marked by keyword &#8220;<strong>override<\/strong>&#8220;\u009d<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>If parent method is marked by keyword &#8220;Virtual&#8221;\u009d but <strong>child is not marked<\/strong> by keyword &#8220;override&#8221;\u009d, then program will compile but the parent method will not be overrided.<\/li>\n<li>If Child method is marked by keyword &#8220;override&#8221;\u009d <strong>but parent method is not marked by keyword &#8220;virtual&#8221;\u009d <\/strong>then program will not compile. It will give following error :<br \/>\n&#8216;OOPS_Concept.TestB.display()&#8217;: cannot override inherited member &#8216;OOPS_Concept.TestA.display()&#8217;<strong> because it is not marked virtual, abstract, or override.<\/strong><\/li>\n<\/ol>\n<p>Example with keyword Virtual and override:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace OOPS_Concept\n{\n    class TestA\n    {\n        public virtual void display() { Console.WriteLine(&quot;TestA - display()&quot;); }\n    }\n\n    class TestB : TestA\n    {\n        public override void display() { Console.WriteLine(&quot;TestB - display()&quot;); }\n    }\n\n    class Test\n    {\n        static void Main(string&#x5B;] args)\n        {\n            TestA a;\n            TestB b;\n\n            a = new TestA();\n            b = new TestB();\n            a.display();  \/\/ TestA - display()\n            b.display();  \/\/ TestB - display()\n\n            a = new TestB();\n            a.display();  \/\/ TestA - display()\n\n            Console.Read();\n        }\n    }\n}\n<\/pre>\n<p>output:<\/p>\n<blockquote><p>TestA &#8211; display()<br \/>\nTestB &#8211; display()<br \/>\nTestB &#8211; display()<\/p><\/blockquote>\n<hr \/>\n<p><strong>Method Hiding \u2013 Keyword new:<\/strong><br \/>\nAs discussed earlier, second point in output of first program was &#8220;Compiler generated warning message&#8221;\u009d.<br \/>\nBecause c# also support Method Hiding. To mark method as &#8220;hiding&#8221;\u009d use keyword &#8220;new&#8221;\u009d in derived class .<br \/>\nKeyword &#8220;new&#8221;\u009d can be used with keyword &#8220;virtual&#8221;\u009d also.<br \/>\nQuick Note :<br \/>\nIf keyword &#8220;override&#8221;\u009d is used in derive class then its override the parent method.<br \/>\nIf Keyword &#8220;new&#8221;\u009d is used in derive class then derive method hided by parent method. As shown in below program:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace OOPS_Concept\n{\n    class TestA\n    {\n        public void display() { Console.WriteLine(&quot;TestA - display()&quot;); }\n    }\n\n    class TestB : TestA\n    {\n        public virtual new void display() { Console.WriteLine(&quot;TestB - display()&quot;); }\n        \/\/Instead of virtual new, new can also be written.\n    }\n\n    class TestC : TestB\n    {\n        public new void display() { Console.WriteLine(&quot;TestC - display()&quot;); }\n    }\n\n    class Test\n    {\n        static void Main(string&#x5B;] args)\n        {\n            TestB b;\n            TestC c;\n\n            b = new TestB();\n            b.display();  \/\/ TestB - display()\n\n            b = new TestC();\n            b.display();  \/\/ TestB - display() instead of TestC - display()\n\n            Console.Read();\n        }\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<blockquote><p>TestB &#8211; display()<br \/>\nTestB &#8211; display()<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Virtual, Override and new Keyword : Polymorphism , Method Hiding and Overriding in C#<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22],"tags":[51],"class_list":["post-1933","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1356,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/final-keyword-in-c-sealed-with-const-and-readonly\/","url_meta":{"origin":1933,"position":0},"title":"Final Keyword in C# &#8211; sealed with const and readonly","author":"Jitendra","date":"April 7, 2011","format":false,"excerpt":"Example of Final Keyword in C# - sealed with const and readonly","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":702,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","url_meta":{"origin":1933,"position":1},"title":"Interface, Inheritance and abstract class","author":"Jitendra","date":"August 6, 2010","format":false,"excerpt":"What is abstract class, interface and inheritance in java. Source code and example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inheritance, Interface and abstract class","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1937,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/virtual-function-in-java\/","url_meta":{"origin":1933,"position":2},"title":"Virtual Function in JAVA","author":"Jitendra","date":"April 8, 2011","format":false,"excerpt":"Example and explanation of Virtual Function in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":414,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/background-and-foreground-thread-in-c\/","url_meta":{"origin":1933,"position":3},"title":"Background and foreground thread in c#","author":"Jitendra","date":"June 29, 2010","format":false,"excerpt":"Example of Threading in C#. Includes Foreground and Background Threading.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Thread Foreground Background","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/ThreadDemo-300x93.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1655,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/introduction-to-annotation-in-java\/","url_meta":{"origin":1933,"position":4},"title":"Introduction to Annotation in JAVA","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Introduction to Annotation in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1629,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/nested-class-and-its-necessity-with-example\/","url_meta":{"origin":1933,"position":5},"title":"Nested Class and its necessity with example","author":"Jitendra","date":"March 11, 2011","format":false,"excerpt":"Example and Introduction of Nested classes in JAVA and there Types","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Nested Class in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Nested-Class-in-JAVA.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1933"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}