{"id":164,"date":"2009-07-24T12:59:48","date_gmt":"2009-07-24T11:59:48","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=164"},"modified":"2009-07-24T12:59:48","modified_gmt":"2009-07-24T11:59:48","slug":"class-structure","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/class-structure\/","title":{"rendered":"Class structure"},"content":{"rendered":"<p>This is a general tutorial about the class structure of programming languages.<\/p>\n<p>The class is basically an object that allows public methods (accessed outside that class object) and private methods (the internal class logic) and protected with is the same as private apart from an inherited class can access the protected objects.  Basically the 3 main types<\/p>\n<p>Public      : Access from outside the class, and internal of course, with inherited\/friends etc.<br \/>\nProtected : Access from inherited\/friends of the class, but not called from the class object.<br \/>\nPrivate     : Access from only the class and friends of the class.<\/p>\n<p>For example within C# coding &#8230;<\/p>\n<pre lang=\"csharp\">\r\nclass example1\r\n{\r\n       private int value1;\r\n       private int value2;\r\n       protected int value3;\r\n       \r\n       public int addtwo(int a, int b)\r\n       {\r\n              value1 = a;\r\n              value2 = b;\r\n              value3 = (a+b);              \/\/ setup the local variables\r\n              return value3;\r\n       }\r\n       \r\n       public int returnValue()\r\n       {\r\n              return value1;\r\n       }\r\n}\r\n\r\nclass example2 : example1\r\n{\r\n       public int returnValue2()\r\n       {\r\n              return value2;       \/\/ will error due to value2 is private in the inherited class\r\n              \/\/return 0;              \/\/ to be able to compile\r\n       }\r\n       \r\n       public int returnValue3()\r\n       {\r\n              return value3;       \/\/ fine since a protected part of the inherited class\r\n       }\r\n}\r\n\r\nclass examples \r\n{\r\n       static public void Main()\r\n       {\r\n              example1 ex = new example1();\r\n              example2 ex2 = new example2();\r\n              \/\/ add the two numbers and set the local internal variables\r\n              System.Console.WriteLine(\"Two numbers 3 + 4 = \" + ex.addtwo(3,4));\r\n              \/\/ output the value1 from the internal example1 class\r\n              System.Console.WriteLine(\"Value1 = \" + ex.returnValue());\r\n              \r\n              \/\/ Just for demo'ing, since the example2 is not linked to example1, just inherites it.\r\n              System.Console.WriteLine(ex2.returnValue2());       \/\/ will not compile the class above.\r\n              System.Console.WriteLine(ex2.returnValue3());\r\n              return;\r\n       }\r\n}\r\n<\/pre>\n<p>save as classexample.cs.  The output will be <\/p>\n<pre class=\"consoleoutput\">\r\nTwo numbers 3 + 4 = 7\r\nValue1 = 3\r\n0\r\n0\r\n<\/pre>\n<p>The two zero&#8217;s are because class example2 only inherits the example class and not does not link to it.  I have added the line within the example2 returnValue2 method to be able to compile the source code otherwise you will get the error similar to <\/p>\n<pre class=\"consoleoutput\">\r\nclassexample.cs(25,10): error CS0122: example1.value2 is inaccessible due to its protection level\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a general tutorial about the class structure of programming languages. The class is basically an object that allows public methods (accessed outside that class object) and private methods (the internal class logic) and protected with is the same as private apart from an inherited class can access the protected objects. Basically the 3 &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/class-structure\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Class structure<\/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":[14,398,13,400],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-c-sharp","tag-linux","tag-mono","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/164","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=164"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}