{"id":514,"date":"2010-01-14T22:53:19","date_gmt":"2010-01-14T22:53:19","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=514"},"modified":"2010-01-14T22:53:19","modified_gmt":"2010-01-14T22:53:19","slug":"inheritance-and-over-riding-methods-c","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/inheritance-and-over-riding-methods-c\/","title":{"rendered":"Inheritance and over riding methods &#8211; c#"},"content":{"rendered":"<p>Inheritance allows one class to be expanded on in further classes.  The protected method\/variables in the base class is accessible to the inherited classes, but the private method\/variables are not.<\/p>\n<p>Here is a very basic inheritance <\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace inheritance\r\n{\r\n\t\r\n\tclass firstClass\r\n\t{\r\n\t\tpublic void printFirstOut()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Hi from the first class\");\r\n\t\t}\r\n\t\t\t\r\n\t}\r\n\t\r\n\tclass secondClass : firstClass\r\n\t{\r\n\t\tpublic void printSecondOut()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Hi from second 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\tsecondClass sec = new secondClass();\r\n\t\t\t\/\/ can still call the first class method\r\n\t\t\tsec.printFirstOut();\r\n\t\t\t\r\n\t\t\t\/\/ and also the second class method of course\r\n\t\t\tsec.printSecondOut();\r\n\t\t\t\r\n\t\t\tfirstClass first = new firstClass();\r\n\t\t\t\/\/ can print out the first class method\r\n\t\t\tfirst.printFirstOut();\r\n\r\n\t\t\t\/\/ but first does not have knowledge of the printSecondOut() method\r\n\t\t\t\/\/ because it is not linked. Error below.\r\n\/\/\t\t\tfirst.printSecondOut();\r\n\t\t\t\/\/ Description=Type `inheritance.firstClass' does not contain a definition for `printSecondOut' \r\n\t\t\t\/\/ and no extension method `printSecondOut' of type `inheritance.firstClass' \r\n\t\t\t\/\/ could be found (are you missing a using directive or an assembly reference?)(CS1061)]\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>output would be<\/p>\n<pre lang=\"bash\">\r\nHi from the first class\r\nHi from second class\r\nHi from the first class\r\n<\/pre>\n<p>Over riding of methods from the base class can happen to make a version 2 of a class as such.<\/p>\n<p>The printClassName() method below is override&#8217;d in the secondClass and it requires to have the word &#8216;new&#8217; because then that tells the compiler to over ride the method in the secondclass, it may come back with a <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/use-the-new-keyword-if-hiding-was-intendedcs0108\/\">warning<\/a> in the compiling of this project to say that there is a same method name in the inherited class.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace inheritance\r\n{\r\n\t\r\n\tclass firstClass\r\n\t{\r\n\t\tpublic void printClassName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"FirstClass\");\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass secondClass : firstClass\r\n\t{\r\n\t\tnew public void printClassName()\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"SecondClass\");\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\tfirstClass first = new firstClass();\r\n\t\t\tfirst.printClassName();\r\n\t\t\t\r\n\t\t\tsecondClass sec = new secondClass();\r\n\t\t\tsec.printClassName();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>output would be<\/p>\n<pre lang=\"bash\">\r\nFirstClass\r\nSecondClass\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance allows one class to be expanded on in further classes. The protected method\/variables in the base class is accessible to the inherited classes, but the private method\/variables are not. Here is a very basic inheritance using System; namespace inheritance { class firstClass { public void printFirstOut() { Console.WriteLine(&#8220;Hi from the first class&#8221;); } } &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/14\/inheritance-and-over-riding-methods-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Inheritance and over riding methods &#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":[56],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-inheritance"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/514","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=514"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions\/515"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}