{"id":223,"date":"2022-01-22T15:23:25","date_gmt":"2022-01-22T15:23:25","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=223"},"modified":"2022-01-22T15:24:02","modified_gmt":"2022-01-22T15:24:02","slug":"scala-traits","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-traits\/","title":{"rendered":"Scala &#8211; Traits"},"content":{"rendered":"<p>Similar to an interface, a trait in Scala is a collection of abstract and non-abstract methods. A trait can be created with all of its methods abstract. In Scala, a class can inherit from more than one traits.<\/p>\n<p>Traits are compiled into Java interfaces with the classes that implements the trait.<\/p>\n<p>Let&#8217;s learn more about traits under the following headings:<\/p>\n<ol>\n<li><a href=\"#t1\">Defining a Trait<\/a><\/li>\n<li><a href=\"#t2\">Another Trait Example<\/a><\/li>\n<li><a href=\"#t3\">Value Classes and Universal Traits<\/a><\/li>\n<li><a href=\"#t4\">When to Use a Trait<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Defining a Trait<\/strong><\/h4>\n<p>Defining a is similar to class definition except that in the case of trait, the keyword <strong>trait<\/strong> is used.\u00a0 The code below defines a trait named PrintText with a print() method. Then the class PrintLine inherits the trait and implements the print() method.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">trait<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PrintText<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> print<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;\">PrintLine<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> print<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;\">\"Welcome to Traits\"<\/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;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">TraitDemo<\/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: #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;\">var<\/span> line <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">PrintLine<\/span><span style=\"color: #333333;\">();<\/span>\r\n    line<span style=\"color: #333333;\">.<\/span>print<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>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Another Trait Example<\/strong><\/h4>\n<p>In the following example we, define a trait, Equal which has two methods: isEqual() and isNotEqual(). The isNotEqual() method has an implementation while the isEqual() method has not. If a class extends this trait, then it must provide implementation for the methods that are not implemented.<\/p>\n<p>The class Point extends the Equal trait and therefore provides the implementation for the isEqual() method.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">trait<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Equal<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> isEqual<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Any<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Boolean<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> isNotEqual<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Any<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Boolean<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">!<\/span>isEqual<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;\">Point<\/span> <span style=\"color: #333333;\">(<\/span>posX<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: #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;\">Equal<\/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> x\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> y\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> isEqual<span style=\"color: #333333;\">(<\/span>obj<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Any<\/span><span style=\"color: #333333;\">)<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Boolean<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> obj<span style=\"color: #333333;\">.<\/span>isInstanceOf<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Point<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> obj<span style=\"color: #333333;\">.<\/span>asInstanceOf<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Point<\/span><span style=\"color: #333333;\">].<\/span>x<span style=\"color: #333333;\">==<\/span> y\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;\">TraitDemo<\/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: #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;\">val<\/span> p1 <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Point<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> p2 <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Point<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> p3 <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Point<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n    println<span style=\"color: #333333;\">(<\/span>p1<span style=\"color: #333333;\">.<\/span>isNotEqual<span style=\"color: #333333;\">(<\/span>p2<span style=\"color: #333333;\">))<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>p1<span style=\"color: #333333;\">.<\/span>isNotEqual<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">))<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>p2<span style=\"color: #333333;\">.<\/span>isEqual<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/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>Note that the two points should be equal when x values are equal. Therefore, the output of the code would be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">true\r\ntrue\r\nfalse\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Value Classes and Universal Traits<\/strong><\/h4>\n<p>Value classes is a mechanism in Scala to prevent allocation of runtime objects. It contains a primary constructor with only one val parameter. Moreover, it contains only methods. It does not contain variable defined, like var or val. Besides, it does not contain any other objects or nested classes. Value classes cannot be extended by another class. This is made possible by extending a value class with <strong>AnyVal<\/strong>.<\/p>\n<p>Normally, a value class cannot extend trait. However, this can be permitted by the concept of <strong>universal traits.<\/strong> So for a value class to extend a trait, the trait has to be converted to a universal trait by making it extend <strong>Any<\/strong>.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Universal trait definition<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">trait<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Printable<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Any<\/span> <span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> print<span style=\"color: #333333;\">()<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">this<\/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: #888888;\">\/\/Class that extends the Printable trait via AnyVal<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Wrapper<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">val<\/span> theValue<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;\">AnyVal<\/span> <span style=\"color: #008800; font-weight: bold;\">with<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Printable<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">TraitDemo<\/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: #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;\">val<\/span> w <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Wrapper<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">)<\/span>\r\n    w<span style=\"color: #333333;\">.<\/span>print<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. When Traits Can be Used<\/strong><\/h4>\n<p>Traits can be used in the following scenarios:<\/p>\n<ul>\n<li>when the behaviour is not expected to be reused, then the class can be made a concrete class<\/li>\n<li>if the behaviour needs to be reused in several unrelated classes, then it could be made a trait<\/li>\n<li>if you need to inherit behaviour from Java code, then you need to use an abstract class<\/li>\n<li>if efficiency is desired, then a class might be the best option<\/li>\n<li>if you will need to distribute the code in compiled form and outside groups need to write class inheriting from it, then you and abstract class could be the way to go<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Similar to an interface, a trait in Scala is a collection of abstract and non-abstract methods. A trait can be created with all of its &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":[39,37,40,38],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-trait","tag-traits","tag-universal-trait","tag-value-classes"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/223","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=223"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/223\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}