{"id":8507,"date":"2021-04-28T18:00:21","date_gmt":"2021-04-28T18:00:21","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8507"},"modified":"2024-04-13T10:05:13","modified_gmt":"2024-04-13T10:05:13","slug":"scala-access-modifiers","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/","title":{"rendered":"Scala &#8211; Access Modifiers"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;The basics of Scala access modifiers &quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6657,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0,&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">The basics of Scala access modifiers <\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Access Modifiers<\/strong> in <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\"><strong>scala<\/strong> <\/a>are utilized to <a href=\"http:\/\/scala-lang.org\">characterize<\/a> the access field of members of packages, classes or objects in scala. For utilizing an access modifier, you should remember its keyword for the definition of package, class or object. These modifiers will restrict access to the members to the specific part of code.<\/p>\r\n<p>Scala access modifiers control the visibility and accessibility of classes, traits, methods, and fields within a Scala program. There are three primary access modifiers in Scala:<\/p>\r\n<ol>\r\n<li>\r\n<p><strong>Private<\/strong>: Members marked as <code>private<\/code> are accessible only within the enclosing scope, such as a class or object. Private members cannot be accessed from outside the enclosing scope, even by subclasses.<\/p>\r\n<\/li>\r\n<li>\r\n<p><strong>Protected<\/strong>: Members marked as <code>protected<\/code> are accessible within the defining class and its subclasses. Protected members can be accessed by subclasses but not by other classes in the same package.<\/p>\r\n<\/li>\r\n<li>\r\n<p><strong>Public (No Modifier)<\/strong>: Members without explicit access modifiers are <code>public<\/code> by default. Public members are accessible from anywhere within the same package or from external packages.<\/p>\r\n<\/li>\r\n<\/ol>\r\n<p>Additionally, Scala supports package-level access modifiers (<code>private[package]<\/code> and <code>protected[package]<\/code>) that restrict visibility to specific packages.<\/p>\r\n\r\n\r\n\r\n<h4><strong>Types of Access Modifiers<\/strong><\/h4>\r\n\r\n\r\n\r\n<p>Private members<\/p>\r\n\r\n\r\n\r\n<p>Protected members<\/p>\r\n\r\n\r\n\r\n<p>Public members<\/p>\r\n\r\n\r\n\r\n<p><strong>Use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Private Members in Scala<\/strong><\/p>\r\n\r\n\r\n\r\n<p>class Prwatech {<br \/>private var a:Int=7<br \/>def show(){<br \/>a=6<br \/>println(a)<br \/>}<br \/>}<br \/>object access extends App{<br \/>var e=new Prwatech()<br \/>e.show()<\/p>\r\n\r\n\r\n\r\n<p>}<\/p>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>6<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"187\" class=\"wp-image-8511\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png 330w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88-300x170.png 300w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>Use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Protected members in scala<\/strong><\/p>\r\n\r\n\r\n\r\n<p>class prwatech {<br \/>protected var a:Int=7<br \/>def show(){<br \/>a=8<br \/>println(a)<br \/>}<br \/>}<br \/>class prwatech1 extends prwatech{<br \/>def show1(){<br \/>a=9<br \/>println(a)<br \/>}<br \/>}<br \/>object access extends App{<br \/>var e=new prwatech()<br \/>e.show()<br \/>var e1=new prwatech1()<br \/>e1.show1()<br \/>\/\/e.a=10<br \/>\/\/println(e.a)<br \/>}<\/p>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>8<\/p>\r\n\r\n\r\n\r\n<p>9<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"323\" class=\"wp-image-8512\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-89.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-89.png 349w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-89-300x278.png 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>Use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Public members in scala<\/strong><\/p>\r\n\r\n\r\n\r\n<p>class Example {<br \/>var a:Int=10<br \/>}<br \/>object access extends App{<br \/>var e=new Example()<br \/>e.a=5<br \/>println(e.a)<br \/>}<\/p>\r\n\r\n\r\n\r\n<p><strong>output<\/strong>:<\/p>\r\n\r\n\r\n\r\n<p>5<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"132\" class=\"wp-image-8513\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-90.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-90.png 394w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-90-300x101.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/figure>\r\n","protected":false},"excerpt":{"rendered":"<p>The basics of Scala access modifiers &nbsp; Access Modifiers in scala are utilized to characterize the access field of members of packages, classes or objects in scala. For utilizing an access modifier, you should remember its keyword for the definition of package, class or object. These modifiers will restrict access to the members to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1253,1260,1263,1262,1254,1264,1255,1258,1256,1259],"class_list":["post-8507","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-access-modifier","tag-access-modifier-definition","tag-modifiers-of-scala","tag-scala-access-modifier-types","tag-scala-access-modifiers","tag-scala-access-modifiers-use-cases","tag-scala-private-members","tag-scala-protected-members","tag-scala-public-members","tag-types-of-access-modifiers-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The basics of Scala access modifiers - Prwatech<\/title>\n<meta name=\"description\" content=\"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The basics of Scala access modifiers - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-28T18:00:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T10:05:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/\",\"name\":\"The basics of Scala access modifiers - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png\",\"datePublished\":\"2021-04-28T18:00:21+00:00\",\"dateModified\":\"2024-04-13T10:05:13+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png\",\"width\":330,\"height\":187},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Access Modifiers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The basics of Scala access modifiers - Prwatech","description":"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"The basics of Scala access modifiers - Prwatech","og_description":"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-04-28T18:00:21+00:00","article_modified_time":"2024-04-13T10:05:13+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/","name":"The basics of Scala access modifiers - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png","datePublished":"2021-04-28T18:00:21+00:00","dateModified":"2024-04-13T10:05:13+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master The Basics of Scala Access Modifiers - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-88.png","width":330,"height":187},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-access-modifiers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Access Modifiers"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8507"}],"version-history":[{"count":7,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8507\/revisions"}],"predecessor-version":[{"id":11447,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8507\/revisions\/11447"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}