{"id":186,"date":"2022-01-03T16:53:21","date_gmt":"2022-01-03T16:53:21","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=186"},"modified":"2022-01-03T16:53:21","modified_gmt":"2022-01-03T16:53:21","slug":"scala-access-modifiers","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-access-modifiers\/","title":{"rendered":"Scala &#8211; Access Modifiers"},"content":{"rendered":"<p>In this lesson we would be covering the three access modifiers in Scala. They are private, protected and default.<\/p>\n<p>For private and protected, objects and classes are labeled with these keywords. But when no specified keyword is used, then the default is public.<\/p>\n<ol>\n<li><a href=\"#t1\">Private Modifier<\/a><\/li>\n<li><a href=\"#t2\">Protected Modifier<\/a><\/li>\n<li><a href=\"#t3\">Public Modifier<\/a><\/li>\n<li><a href=\"#t4\">Scope of Protection<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Private Modifier<\/strong><\/h4>\n<p>A private member of a class (method or variable) is visible only within the class or object containing the definition.<\/p>\n<p>The code below shows a private method in a class<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Parent<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Child<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #008800; font-weight: bold;\">def<\/span> show<span style=\"color: #333333;\">()<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I am the child\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">grandChild<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      show<span style=\"color: #333333;\">();<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> child1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Child<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Child<\/span><span style=\"color: #333333;\">();<\/span>\r\n  child1<span style=\"color: #333333;\">.<\/span>show<span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/Error: the show() method is private<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>In this example, the show method is defined in the Child class and it is private. This means that it can only be access from within the Child class, not from outside. However, the grandChild class is inside the Child class and show the show() method is accessible from there too.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Protected Modifier<\/strong><\/h4>\n<p>Protected members are accessible from within the class and from classes that are derived from that class.<\/p>\n<p>An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Parent<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Child<\/span><span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #008800; font-weight: bold;\">def<\/span> show<span style=\"color: #333333;\">()<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">={<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I am the child\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Sib<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Child<\/span><span style=\"color: #333333;\">(){<\/span>\r\n    show<span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/ this works fine because Sib extends Class<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n  \r\n  <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Uncle<\/span><span style=\"color: #333333;\">(){<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> child1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Child<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Child<\/span><span style=\"color: #333333;\">();<\/span>\r\n    child1<span style=\"color: #333333;\">.<\/span>show<span style=\"color: #333333;\">();<\/span> <span style=\"color: #888888;\">\/\/ This fails because Uncle does not extend Child<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>The comment explain this. Try to change the Uncle to make it extend Child, then see that it works.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Public Modifier<\/strong><\/h4>\n<p>This is the default access modifier. For public members, you don&#8217;t need to specify the public keyword. Once the keyword is not specified, the the it is public. The code below demonstrates this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">AccessDemo<\/span><span style=\"color: #333333;\">{<\/span>  \r\n     <span style=\"color: #008800; font-weight: bold;\">var<\/span> x<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>  \r\n     <span style=\"color: #008800; font-weight: bold;\">def<\/span> show<span style=\"color: #333333;\">(){<\/span>  \r\n         println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\" x = \"<\/span><span style=\"color: #333333;\">+<\/span>x<span style=\"color: #333333;\">)<\/span>  \r\n     <span style=\"color: #333333;\">}<\/span>  \r\n<span style=\"color: #333333;\">}<\/span>  \r\n  \r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">MainObject<\/span><span style=\"color: #333333;\">{<\/span>  \r\n    <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]){<\/span>  \r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> x <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">AccessDemo<\/span><span style=\"color: #333333;\">()<\/span>  \r\n        x<span style=\"color: #333333;\">.<\/span>show<span style=\"color: #333333;\">()<\/span>  \r\n    <span style=\"color: #333333;\">}<\/span>  \r\n<span style=\"color: #333333;\">}<\/span>  \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Scope of Protection<\/strong><\/h4>\n<p>The private and protected access modifiers can be enhanced using qualifiers. This qualifiers are specified in square brackets following the modifier. For example, we can have private[X] or protected[X]. This means that the modifier applies up to X, where X is some enclosing package, class or singleton object.<\/p>\n<p>An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">students<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">package<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">teachers<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Executive<\/span> <span style=\"color: #333333;\">{<\/span>\r\n         <span style=\"color: #008800; font-weight: bold;\">private<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">students<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">var<\/span> workDetails <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span>\r\n\r\n         <span style=\"color: #008800; font-weight: bold;\">private<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">teachers<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">var<\/span> friends <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span>\r\n\r\n         <span style=\"color: #008800; font-weight: bold;\">private<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">var<\/span> secrets <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span>\r\n\r\n         <span style=\"color: #008800; font-weight: bold;\">def<\/span> test<span style=\"color: #333333;\">(<\/span>another <span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Executive<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n            println<span style=\"color: #333333;\">(<\/span>another<span style=\"color: #333333;\">.<\/span>workDetails<span style=\"color: #333333;\">)<\/span>\r\n            println<span style=\"color: #333333;\">(<\/span>another<span style=\"color: #333333;\">.<\/span>secrets<span style=\"color: #333333;\">)<\/span> <span style=\"color: #888888;\">\/\/This fails!!<\/span>\r\n         <span style=\"color: #333333;\">}<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>From the code below, we note the following points:<\/p>\n<ul>\n<li>the member variable workDetails is accessible to any class within the students package<\/li>\n<li>the variable friends is accessible within the teachers package<\/li>\n<li>the variable secrets is accessible only on the object within the instance methods (this)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson we would be covering the three access modifiers in Scala. They are private, protected and default. For private and protected, objects and &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[27,29,30,28],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-access-modifiers","tag-private","tag-protected","tag-public"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/186\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}