{"id":5721,"date":"2020-10-08T05:46:13","date_gmt":"2020-10-08T05:46:13","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=5721"},"modified":"2024-04-12T10:20:24","modified_gmt":"2024-04-12T10:20:24","slug":"encapsulation-in-scala-object-oriented-programming-in-scala","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/","title":{"rendered":"Scala &#8211; Encapsulation"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Object Oriented Programming in Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Object Oriented Programming in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>The <em><strong>Inner<\/strong><\/em> class means characterizing a class into another. This element empowers the users to sensibly bunch classes that are just utilized in one place, subsequently this expands the utilization of encapsulation, and make more creatable and viable code. In <strong><a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala<\/a><\/strong>, the idea of inner classes is not <a href=\"https:\/\/www.scala-lang.org\/\">the<\/a> same as Java. Like in Java, the inner class is the member from the external class, yet in Scala, the internal class to the outer object.<\/p>\r\n<p>Object-Oriented Programming (OOP) is a foundational paradigm in Scala that emphasizes the organization of code into classes and objects, promoting encapsulation, inheritance, and polymorphism. Scala seamlessly combines functional and object-oriented programming concepts, providing a powerful and expressive language for building modular and extensible applications.<\/p>\r\n<p>In Scala, classes serve as blueprints for creating objects, which encapsulate state (fields) and behavior (methods). Scala supports single and multiple inheritance through traits, enabling code reuse and flexibility in class hierarchies. Traits provide a mechanism for composing behaviors independently of class inheritance.<\/p>\r\n<p>Scala&#8217;s type system supports subtype polymorphism, allowing methods to be overridden in subclasses to provide specialized implementations. Furthermore, Scala encourages immutability and composition over inheritance, promoting a more flexible and robust design approach.<\/p>\r\n\r\n\r\n\r\n<p><strong>Use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\r\n\/\/ Outer class\r\nclass prwatech {\r\n\/\/ Inner class\r\nclass P1\r\n  {\r\n       var a = 1\r\n       def method()\r\n     {\r\n          for(a&lt;-0 to 2)\r\n        {\r\n           println(\"Welcome to Prwatech Data Science\");\r\n        }\r\n     }\r\n  }\r\n}\r\nobject Main\r\n{\r\n       def main(args: Array[String])\r\n   { \r\n       \/\/ Creating object of the outer and\r\n      \/\/ inner class Here, P1 class is \r\n      \/\/ bounded with the object of Prwatech class\r\n      val obj = new prwatech();\r\n      val o = new obj.P1;\r\n      o.method();\r\n   }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Output<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Welcome to Prwatech Data Science<br \/>2Welcome to Prwatech Data Science<br \/>Welcome to Prwatech Data Science<\/p>\r\n\r\n\r\n\r\n<p><strong>Use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\r\n\/\/ Class inside Object\r\nclass outer_class_Prwatech\r\n{\r\n    object inner_object_Prwatech1\r\n    {\r\n        val a = 0;\r\n        def method()\r\n        {\r\n            for(a &lt;- 1 to 2)\r\n            {\r\n                println(\"object inside a class Prwatech\")\r\n            }\r\n            println()\r\n        }\r\n    }\r\n}\r\n  \r\n\/\/ Object inside Class\r\nobject outer_objectPrwatech\r\n{\r\n    class innerPrwatech\r\n    {\r\n        val b = 0;\r\n        def method()\r\n        {\r\n            for(b &lt;- 1 to 2)\r\n            {\r\n                println(\"class inside an object Prwatech\")\r\n            }\r\n        }\r\n    }\r\n}\r\n  \r\nobject Main\r\n{\r\n      \r\n    \/\/ Main method\r\n    def main(args: Array[String])\r\n    {\r\n          \r\n        \/\/ Object inside a class\r\n        new outer_class_Prwatech().inner_object_Prwatech1.method;\r\n          \r\n        \/\/ Class inside an object\r\n        new outer_objectPrwatech.innerPrwatech().method;\r\n    }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Output<\/strong><\/p>\r\n\r\n\r\n\r\n<p>object inside a class Prwatech<br \/>object inside a class Prwatech<\/p>\r\n\r\n\r\n\r\n<p>class inside an object Prwatech<br \/>class inside an object Prwatech<\/p>\r\n\r\n\r\n\r\n<p><strong>Use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>class Prwatech\r\n{\r\n\/\/ Inner class\r\nclass P1\r\n{\r\n    var x = 1\r\n    def method()\r\n    {\r\n        for(x&lt;-0 to 0  )\r\n        {\r\n            println(\"Welcome to Prwatech\");\r\n        }\r\n    }\r\n}\r\n}\r\nobject Main\r\n{\r\ndef main(args: Array[String])\r\n{\r\nval obj = new Prwatech();\r\n    val o = new obj.P1;\r\n    o.method();\r\n}\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Output: \r\nWelcome to Prwatech\r\n  <\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Programming in Scala &nbsp; The Inner class means characterizing a class into another. This element empowers the users to sensibly bunch classes that are just utilized in one place, subsequently this expands the utilization of encapsulation, and make more creatable and viable code. In Scala, the idea of inner classes is not 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":[1273,1265,1271,1266,1267,1268,1269,1270,1272],"class_list":["post-5721","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-scala-inner-method","tag-encapsulation-in-scala","tag-inner-method-in-scala","tag-scala-encapsulation","tag-scala-encapsulation-definition","tag-scala-encapsulation-examples","tag-scala-encapsulation-use-cases","tag-scala-inner-method","tag-what-is-scala-encapsulation"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Object Oriented Programming in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Object-Oriented Programming in Scala - 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=\"Object Oriented Programming in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Object-Oriented Programming in Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/\" \/>\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=\"2020-10-08T05:46:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T10:20:24+00:00\" \/>\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\/encapsulation-in-scala-object-oriented-programming-in-scala\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/\",\"name\":\"Object Oriented Programming in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2020-10-08T05:46:13+00:00\",\"dateModified\":\"2024-04-12T10:20:24+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Object-Oriented Programming in Scala - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Encapsulation\"}]},{\"@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":"Object Oriented Programming in Scala - Prwatech","description":"Master Object-Oriented Programming in Scala - 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":"Object Oriented Programming in Scala - Prwatech","og_description":"Master Object-Oriented Programming in Scala - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2020-10-08T05:46:13+00:00","article_modified_time":"2024-04-12T10:20:24+00:00","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\/encapsulation-in-scala-object-oriented-programming-in-scala\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/","name":"Object Oriented Programming in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2020-10-08T05:46:13+00:00","dateModified":"2024-04-12T10:20:24+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Object-Oriented Programming in Scala - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/encapsulation-in-scala-object-oriented-programming-in-scala\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Encapsulation"}]},{"@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\/5721","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=5721"}],"version-history":[{"count":8,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5721\/revisions"}],"predecessor-version":[{"id":11380,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5721\/revisions\/11380"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=5721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=5721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=5721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}