{"id":5716,"date":"2020-10-08T05:35:03","date_gmt":"2020-10-08T05:35:03","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=5716"},"modified":"2024-04-12T10:07:53","modified_gmt":"2024-04-12T10:07:53","slug":"polymorphism-in-scala-polymorphic-types-scala-tutorial","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/","title":{"rendered":"Scala &#8211; Polymorphism"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Polymorphic Types - Scala Tutorial&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;}\">Polymorphic Types &#8211; Scala Tutorial<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Polymorphism<\/strong> is the capability of any <a href=\"https:\/\/prwatech.in\/blog\/#\">data<\/a> to be <a href=\"https:\/\/www.scala-lang.org\/\">process<\/a> in more than one form. Scala executes polymorphism through virtual functions, over-loaded functions and over-loaded operators. Polymorphism is perhaps the main ideas of object orient programming language. The most widely recognized utilization of polymorphism in object oriented programming happens when a parent class reference is utilized to refer to a child class object.Polymorphic Types &#8211; Scala Tutorial<\/p>\r\n\r\n\r\n\r\n<p><em><strong>poly<\/strong><\/em> means <strong>many<\/strong> and <strong><em>morphism<\/em><\/strong> means <strong>types<\/strong><\/p>\r\n\r\n\r\n\r\n<p>There are two forms of polymorphism:<\/p>\r\n\r\n\r\n\r\n<p><strong>Sub typing:<\/strong> In subtyping a subclass\u2019s instance can be passe to a base class<\/p>\r\n\r\n\r\n\r\n<p><strong>Generics:<\/strong> By using type parameterization, instances of a function or class are create .<\/p>\r\n<p>Polymorphic types in Scala refer to the ability to write generic, reusable code that can operate on values of different types while maintaining type safety. Scala supports two primary forms of polymorphism: parametric polymorphism (generics) and subtype polymorphism (inheritance and subtyping).<\/p>\r\n<p>Parametric polymorphism allows developers to define functions and data structures that can operate on values of any type. This is achieved using type parameters, denoted within square brackets (<code>[T]<\/code>), which represent placeholder types that are specified when the function or data structure is used.<\/p>\r\n<p>. Scala supports subtype polymorphism using traits and abstract classes, allowing for the implementation of polymorphic behavior through method overriding and dynamic dispatch.<\/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>class prwatech {\r\n\/\/ This is the first function with the name fun\r\ndef func(a:Int)\r\n{\r\n     println(\"Result:\" + a);\r\n}\r\n\/\/ This is the second function with the name fun\r\n    def func(a:Int, b:Int) \r\n{ \r\n    var sum = a + b; \r\n    println(\"Result of a + b is:\"  + sum); \r\n} \r\n\r\n\/\/ This is the first function with the name fun\r\n   def func(a:Int, b:Int, c:Int) \r\n{ \r\n    var product = a * b * c; \r\n    println(\"Result of a * b * c is:\"  + product); \r\n} \r\n}\r\n\/\/ Creating object\r\nobject Main\r\n{\r\n\/\/ Main method\r\n    def main(args: Array[String])\r\n   {\r\n   \/\/ Creating object of class\r\n   var ob = new prwatech();\r\n   ob.func(10);\r\n   ob.func(8, 8);\r\n   ob.func(1, 3, 6);\r\n   }\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>output:\r\nResult:10\r\nResult of a + b is:16\r\nResult of a * b * c is:18<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>2Use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>class prwatech {\r\n\/\/ This is the first function with the name fun\r\n\/\/ Function 1\r\ndef func(Company:String, Domain:String)\r\n{\r\nprintln(\"Company is:\" + Company: String);\r\nprintln(\"Domain is:\" + Domain: String);\r\n}\r\n\/\/ Function 2 \r\ndef func(name:String, Salary:Int)      \r\n{ \r\n    println(\"Employee Name is:\" + name); \r\n    println(\"Salary is:\" + Salary); \r\n} \r\n\r\n\/\/ Function 3 \r\ndef func(a:Int, b:Int) \r\n  {\r\n    var Sum = a + b;\r\n    println(\"Sum is:\" + Sum)\r\n  }\r\n}\r\n\/\/ Creating object\r\nobject Main\r\n{\r\n\/\/ Main method\r\ndef main(args: Array[String])\r\n{\r\nvar A = new prwatech();\r\nA.func(\"ABC\", \"Data Science\");\r\nA.func(\"xyz\",\"BigData\");\r\nA.func(\"Michael\", 100000 );\r\nA.func(10, 20);\r\n}\r\n}\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Output:\r\nDomain is:Data Science\r\nCompany is:xyz\r\nDomain is:BigData\r\nEmployee Name is:Michael\r\nSalary is:100000\r\nSum is:30\r\n<\/code><\/pre>\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 eduprwa {\r\n\/\/ This is the first function with the name fun\r\ndef func(a:Int)\r\n{\r\n     println(\"Result:\" + a);\r\n}\r\n\/\/ This is the second function with the name fun\r\n    def func(a:Int, b:Int) \r\n{ \r\n    var diff = a - b; \r\n    println(\"Result of a - b is:\"  + diff); \r\n} \r\n\r\n\/\/ This is the first function with the name fun\r\n   def func(a:Int, b:Int, c:Int, d:Float) \r\n{ \r\n    var expression = a * b * c + d  ; \r\n    println(\"Result of a * b \/ c + d is:\"  + expression); \r\n} \r\n}\r\n\/\/ Creating object\r\nobject edu\r\n{\r\n\/\/ Main method\r\n    def main(args: Array[String])\r\n   {\r\n   \/\/ Creating object of class\r\n   var ob = new eduprwa();\r\n   ob.func(10);\r\n   ob.func(8, 8);\r\n   ob.func(1, 3, 6, 5);\r\n   }\r\n}\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Result:10\r\nResult of a - b is:0\r\nResult of a * b \/ c + d is:23.0<br \/><br \/><br \/><br \/><br \/>\r\n<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Polymorphic Types &#8211; Scala Tutorial &nbsp; Polymorphism is the capability of any data to be process in more than one form. Scala executes polymorphism through virtual functions, over-loaded functions and over-loaded operators. Polymorphism is perhaps the main ideas of object orient programming language. The most widely recognized utilization of polymorphism in object oriented programming happens [&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":[1288,1291,1285,1283,1289,1284,1286,1290,1287,1292],"class_list":["post-5716","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-polymorphism","tag-polymorphism-concept","tag-polymorphism-examples","tag-polymorphism-in-scala","tag-polymorphism-use-cases-in-scala","tag-scala-polymorphism-definition","tag-scala-usecases-of-polymorphism","tag-types-of-polymorphism","tag-what-is-polymorphism","tag-what-is-scala-polymorphism"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Polymorphic Types - Scala Tutorial - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.\" \/>\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=\"Polymorphic Types - Scala Tutorial - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/\" \/>\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:35:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T10:07:53+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=\"1 minute\" \/>\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\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/\",\"name\":\"Polymorphic Types - Scala Tutorial - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2020-10-08T05:35:03+00:00\",\"dateModified\":\"2024-04-12T10:07:53+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Polymorphism\"}]},{\"@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":"Polymorphic Types - Scala Tutorial - Prwatech","description":"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.","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":"Polymorphic Types - Scala Tutorial - Prwatech","og_description":"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2020-10-08T05:35:03+00:00","article_modified_time":"2024-04-12T10:07:53+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/","name":"Polymorphic Types - Scala Tutorial - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2020-10-08T05:35:03+00:00","dateModified":"2024-04-12T10:07:53+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Polymorphic Types - Scala Tutorial - Dive deep with our expert instructors and comprehensive curriculum, Prwatech.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/polymorphism-in-scala-polymorphic-types-scala-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Polymorphism"}]},{"@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\/5716","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=5716"}],"version-history":[{"count":10,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5716\/revisions"}],"predecessor-version":[{"id":11376,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5716\/revisions\/11376"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=5716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=5716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=5716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}