{"id":522,"date":"2010-01-15T11:53:48","date_gmt":"2010-01-15T11:53:48","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=522"},"modified":"2010-02-02T11:18:31","modified_gmt":"2010-02-02T11:18:31","slug":"polymorphism-override-csharp","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/15\/polymorphism-override-csharp\/","title":{"rendered":"Overrideing polymorphism &#8211; c#"},"content":{"rendered":"<p>Overrideing is also similar to <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/overloading-methods-c\/\">overloading<\/a> (sometimes it can be called the same thing since you are overloading\/polymorphism functions and classes). <\/p>\n<p>Polymorphism is when you implement functions that are defined in the base class, overriding is when you over ride a base class function.<\/p>\n<p>But with Overrideing classes in c# you can override functions from the base class that are declared as virtual.  Virtual means that they are capable of being overridden in a inherited class, so that incase someone tries to call a method of a same name in a subclass then the base class is still called e.g.  sometimes better using code and output to show more than what words can say.<\/p>\n<p>Here is not using the virtual keyword in the base class, so that when you try to call the subclasses same method it still goes to the base class.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace polymorphism\r\n{\r\n\tclass Shape {\r\n\t\tpublic void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Shape base class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Circle : Shape {\r\n\t\tpublic new void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Circle class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Rectangle : Shape {\r\n\t\tpublic new void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Rectangle class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Line : Shape {\r\n\t\tpublic new void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Line class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main(string[] args)\r\n\t\t{\r\n\t\t\tShape[] shapesArray = new Shape[4];\r\n\t\t\tshapesArray[0] = new Shape();\r\n\t\t\tshapesArray[1] = new Circle();\r\n\t\t\tshapesArray[2] = new Rectangle();\r\n\t\t\tshapesArray[3] = new Line();\r\n\t\t\t\r\n\t\t\tforeach (Shape shape in shapesArray)\r\n\t\t\t{\r\n\t\t\t\tshape.printName();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>output would <\/p>\n<pre lang=\"bash\">\r\nShape base class\r\nShape base class\r\nShape base class\r\nShape base class\r\n<\/pre>\n<p>but with the <\/p>\n<pre lang=\"csharp\">\r\nvirtual -  override\r\n<\/pre>\n<p>keywords.<\/p>\n<p>The code<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace polymorphism\r\n{ \r\n\tclass Shape {\r\n\t\tpublic virtual void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Shape base class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Circle : Shape {\r\n\t\tpublic override void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Circle class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Rectangle : Shape {\r\n\t\tpublic override  void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Rectangle class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass Line : Shape {\r\n\t\tpublic override  void printName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Line class\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main(string[] args)\r\n\t\t{\r\n\t\t\tShape[] shapesArray = new Shape[4];\r\n\t\t\tshapesArray[0] = new Shape();\r\n\t\t\tshapesArray[1] = new Circle();\r\n\t\t\tshapesArray[2] = new Rectangle();\r\n\t\t\tshapesArray[3] = new Line();\r\n\t\t\t\r\n\t\t\tforeach (Shape shape in shapesArray)\r\n\t\t\t{\r\n\t\t\t\tshape.printName();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>As expected the printName() function was called from the subclasses, because of the virtual keyword.<\/p>\n<pre lang=\"bash\">\r\nShape base class\r\nCircle class\r\nRectangle class\r\nLine class\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overrideing is also similar to overloading (sometimes it can be called the same thing since you are overloading\/polymorphism functions and classes). Polymorphism is when you implement functions that are defined in the base class, overriding is when you over ride a base class function. But with Overrideing classes in c# you can override functions from &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/15\/polymorphism-override-csharp\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Overrideing polymorphism &#8211; c#<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[58,59],"class_list":["post-522","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-overloading","tag-polymorphism"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":5,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":646,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions\/646"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}