{"id":183,"date":"2022-01-03T11:38:37","date_gmt":"2022-01-03T11:38:37","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=183"},"modified":"2022-01-03T11:38:37","modified_gmt":"2022-01-03T11:38:37","slug":"scala-classes-and-objects","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-classes-and-objects\/","title":{"rendered":"Scala &#8211; Classes and Objects"},"content":{"rendered":"<p>In this lesson, we would be learning about Object Oriented Programming(OOP) concepts in Scala.<\/p>\n<p><strong>A Class<\/strong> is a template or blueprint for creating objects. Once you have defined a class, you can then create objects from the class using the <em><strong>new<\/strong><\/em> keyword.<\/p>\n<p><strong>An Object<\/strong> is an instance of a class. Multiple objects can be created from a class.<\/p>\n<ol>\n<li><a href=\"#t1\">Defining a Class<\/a><\/li>\n<li><a href=\"#t2\">Extending a Class<\/a><\/li>\n<li><a href=\"#t3\">Implicit Classes<\/a><\/li>\n<li><a href=\"#t4\">Singleton Objects<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Defining a Class<\/strong><\/h4>\n<p>Defining a class in Scala is similar to defining a class in Java. The code below defines a class called Square. The class has two variables: l and w. It also has a method called reSize() which takes two parameters.<\/p>\n<p>By default, the class has an implicit constructor with is the name of the class. This constructor takes two arguments: length and width which are visible to the whole body of the 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;\">Square<\/span><span style=\"color: #333333;\">(<\/span>length<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> width<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: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> l<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> length<span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> w<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> width<span style=\"color: #333333;\">;<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> reSize<span style=\"color: #333333;\">(<\/span>l1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> w1<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: #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    l <span style=\"color: #008800; font-weight: bold;\">=<\/span> l <span style=\"color: #333333;\">+<\/span> l1<span style=\"color: #333333;\">;<\/span>\r\n    w <span style=\"color: #008800; font-weight: bold;\">=<\/span> w <span style=\"color: #333333;\">+<\/span> w1<span style=\"color: #333333;\">;<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New length: \"<\/span> <span style=\"color: #333333;\">+<\/span> l<span style=\"color: #333333;\">);<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New width: \"<\/span> <span style=\"color: #333333;\">+<\/span> w<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<p>Having defined this class, you can then use it in main program. I call the program, SquareDemo and it is of type, object as show below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">SquareDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\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> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> sq <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Square<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">43<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">90<\/span><span style=\"color: #333333;\">)<\/span>\r\n    sq<span style=\"color: #333333;\">.<\/span>reSize<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">);<\/span>\r\n\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=\"t2\">2. Extending a Class<\/strong><\/h4>\n<p>Just like in Java, you can extend a class in Scala as well. To achieve this, you need to use the <strong>extends<\/strong> keyword in the new class.<\/p>\n<p>In the program below we have a parent class <strong>Shape<\/strong> which has two variables <strong>x<\/strong> and<strong> y<\/strong> as well as a method move(). Then we have a child class <strong>Square<\/strong> that inherits from shape.<\/p>\n<p>The Square class has two variables <strong>l<\/strong> and <strong>w<\/strong> as well as a <em><strong>reSize()<\/strong><\/em> method.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Parent Class (SuperClass)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">val<\/span> posX<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: #008800; font-weight: bold;\">val<\/span> posY<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: #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> posX<span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> y<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> posY<span style=\"color: #333333;\">;<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> move<span style=\"color: #333333;\">(<\/span>dx<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> dy<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: #333333;\">{<\/span>\r\n    x <span style=\"color: #008800; font-weight: bold;\">=<\/span> x <span style=\"color: #333333;\">+<\/span> dx\r\n    y <span style=\"color: #008800; font-weight: bold;\">=<\/span> y <span style=\"color: #333333;\">+<\/span> dy\r\n    println <span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New x position : \"<\/span> <span style=\"color: #333333;\">+<\/span> x<span style=\"color: #333333;\">);<\/span>\r\n    println <span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New y position : \"<\/span> <span style=\"color: #333333;\">+<\/span> y<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: #888888;\">\/\/Child Class (Subclass or Inherited Class)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Square<\/span><span style=\"color: #333333;\">(<\/span>length<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> width<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: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">){<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> l<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> length<span style=\"color: #333333;\">;<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> w<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> width<span style=\"color: #333333;\">;<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> reSize<span style=\"color: #333333;\">(<\/span>l1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">,<\/span> w1<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: #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    l <span style=\"color: #008800; font-weight: bold;\">=<\/span> l <span style=\"color: #333333;\">+<\/span> l1<span style=\"color: #333333;\">;<\/span>\r\n    w <span style=\"color: #008800; font-weight: bold;\">=<\/span> w <span style=\"color: #333333;\">+<\/span> w1<span style=\"color: #333333;\">;<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New length: \"<\/span> <span style=\"color: #333333;\">+<\/span> l<span style=\"color: #333333;\">);<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New width: \"<\/span> <span style=\"color: #333333;\">+<\/span> w<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<p>The keyword extends used in the subclass:<\/p>\n<ul>\n<li>makes the Square class inherit all the members of the Shape class that are non-private. So the Square class also has members: x, y and move().<\/li>\n<li>makes the Square class a type of Shape<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Implicit Classes<\/strong><\/h4>\n<p>Implicit class is a new feature introduced with Scala 2.10. It is achieved using the implicit keyword when creating the class. This keyword makes the class&#8217; primary constructor available for implicit conversion if the class is in scope.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Helpers<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">implicit<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">IntWithTimes<\/span><span style=\"color: #333333;\">(<\/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: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">def<\/span> times<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">A<\/span><span style=\"color: #333333;\">](<\/span>f<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333333;\">=&gt;<\/span> A<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      <span style=\"color: #008800; font-weight: bold;\">def<\/span> loop<span style=\"color: #333333;\">(<\/span>current<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: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>current <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n          f\r\n          loop<span style=\"color: #333333;\">(<\/span>current <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span>\r\n        <span style=\"color: #333333;\">}<\/span>\r\n      loop<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<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>In this example, we create an implicit class IntWithTimes. This class then wraps an Int value and give a\u00a0 new method times().<\/p>\n<p>To use this class, we simply need to import it into the scope and call it&#8217;s times method as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Demo<\/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> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #0000dd; font-weight: bold;\">6<\/span> times println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Welcome!\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>This would print\u00a0 the text &#8220;Welcome&#8221; 6 times to the output.<\/p>\n<p><strong>Restrictions on Implicit Classes<\/strong><\/p>\n<ul>\n<li>they must be defined inside another class, object or trait<\/li>\n<li>they can only take one non-implicit argument in their constructor<\/li>\n<li>they cannot be any method, member or object in scope with the same name as the implicit class<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Singleton Objects in Scala<\/strong><\/h4>\n<p>The Singleton Pattern is a software engineering pattern that specifies that a class should only have on single instance.<\/p>\n<p>Therefore in Scala, a singleton class is a class that allows instantiation of just one object. So you cannot have static members. And since you cannot instantiate a singleton object, you cannot also pass parameters to the primary constructor.<\/p>\n<p>The examples we&#8217;ve considered previously are all singleton classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would be learning about Object Oriented Programming(OOP) concepts in Scala. A Class is a template or blueprint for creating objects. Once &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":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-scala-programming"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/183","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=183"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/183\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}