

{"id":16793,"date":"2018-05-28T06:10:37","date_gmt":"2018-05-28T06:10:37","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=16793"},"modified":"2021-12-04T10:15:42","modified_gmt":"2021-12-04T04:45:42","slug":"scala-method-overloading","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/","title":{"rendered":"Scala Method Overloading with Example"},"content":{"rendered":"<p>In our previous <a href=\"https:\/\/data-flair.training\/blogs\/scala-tutorial\/\"><strong>Scala tutorial<\/strong><\/a>, we had discussed <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-case-class\/\">Scala Case Class<\/a><\/strong>, now we are going to study Scala Method Overloading. In addition, we will learn example of Overloading Methods in Scala.<br \/>\nLet\u2019s begin Scala Method Overloading.<\/p>\n<h3 class=\"western\">What is Scala Method Overloading?<\/h3>\n<p>Scala Method overloading is when one class has more than one method with the same name but different signature. This means that they may differ in the number of parameters, <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-data-types\/\">data types<\/a><\/strong>, or both. This makes for optimized code.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-string-method\/\">Let&#8217;s discuss Scala String Method with Syntax and Method<\/a><\/strong><\/p>\n<h3 class=\"western\">Example of Overloading Method in Scala<\/h3>\n<p>Here, we are going to understand Scala Method Overloading with examples.<\/p>\n<h3><strong>Example &#8211; 1\u00a0<\/strong><\/h3>\n<p>Let\u2019s take an example with a different number of parameters.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class sayHello{\r\n     | def hello(){\r\n     | println(\"Hello, user\")\r\n     | }\r\n     | def hello(admin:String){\r\n     | println(\"Hello, \"+admin)\r\n     | }\r\n     | def hello(admin:String,guest:String){\r\n     | println(\"Hello, \"+admin+\", Hello \"+guest)\r\n     | }\r\n     | }\r\ndefined class sayHello\r\nscala&gt; var h=new sayHello()\r\nh: sayHello = sayHello@406ad6d5<\/pre>\n<p>We defined a new object \u2018h\u2019 for this class. Now, what this prints depends on which version of the method it calls. This depends on how many parameters we pass it (which version of the method definition satisfies the call format).<\/p>\n<p>While one version addresses the admin, another address both- the admin and the guest. The default version just says hello to user.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-access-modifiers\/\">Read about Scala Access Modifiers: Public, Private and Protected Members<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; h.hello()<\/pre>\n<p>Hello, user<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; h.hello(\"Ayushi\")<\/pre>\n<p>Hello, Ayushi<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; h.hello(\"Ayushi\",\"Megha\")<\/pre>\n<p>Hello, Ayushi, Hello Megha<\/p>\n<h3><strong>Example &#8211; 2<\/strong><\/h3>\n<p>Now, let\u2019s try Scala overloading method with versions owning different types. This way, we can carry out the same functionality on different types depending on what the user asks for.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; class Calculator{\r\n     | def sum(a:Int,b:Int){\r\n     | var sum=a+b\r\n     | println(sum)\r\n     | }\r\n     | def sum(a:Double,b:Double){\r\n     | var sum=a+b\r\n     | println(sum)\r\n     | }\r\n     | }\r\ndefined class Calculator\r\nscala&gt; var c=new Calculator()\r\nc: Calculator = Calculator@1d3e5a05\r\nscala&gt; c.sum(1,2)<\/pre>\n<p>3<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; c.sum(1.1,2.2)<\/pre>\n<p>3.3000000000000003<br \/>\nIn this example, we have two versions of the method \u2018sum\u2019- one that adds two integers, and the other that adds two floats. It then prints out the sum.<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-environment-setup\/\">Let&#8217;s Learn Scala Environment Setup and Get Started with an IDE<\/a><\/strong><br \/>\nSo, this was all about Scala\u00a0Method Overloading tutorial. Hope you like our explanation.<\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Hence, we come to the end of Scala Method Overloading tutorial, we finally knew method\u00a0overloading in Scala with example. Do let us know what you think in the comments.<br \/>\nRelated Article &#8211;\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-string-interpolation\/\">Scala String Interpolation\u00a0<\/a><\/strong><br \/>\n<strong><a href=\"https:\/\/www.scala-lang.org\/\">For reference<\/a><\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1904,&quot;href&quot;:&quot;https:\\\/\\\/www.scala-lang.org&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251206135355\\\/https:\\\/\\\/www.scala-lang.org\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 07:41:44&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-15 07:30:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 15:55:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-23 02:56:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-27 16:24:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-02 02:40:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-05 13:44:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-09 08:06:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 14:24:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-17 17:26:33&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-20 17:57:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-24 15:52:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-30 14:21:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-02 14:58:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-05 16:39:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 17:17:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-16 04:52:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-19 13:39:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-24 17:10:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:44:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 07:25:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-08 21:46:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 18:02:22&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 20:15:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-05 11:05:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-10 19:22:51&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 07:59:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 16:14:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-21 23:18:28&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 09:13:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-29 14:23:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-07 12:16:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-13 10:20:22&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-18 15:54:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-22 05:55:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-25 08:30:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-29 13:12:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-02 14:06:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-09 18:23:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-15 16:29:46&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-15 16:29:46&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous Scala tutorial, we had discussed Scala Case Class, now we are going to study Scala Method Overloading. In addition, we will learn example of Overloading Methods in Scala. Let\u2019s begin Scala&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":16795,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[3710,4341,12453,12495,12515,12521,12557],"class_list":["post-16793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-default-constructor-in-scala","tag-example-of-scala-method-overloading","tag-scala-final-method","tag-scala-method-overloading","tag-scala-operator-overloading","tag-scala-override","tag-scala-this"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Method Overloading with Example - DataFlair<\/title>\n<meta name=\"description\" content=\"Scala Method Overloading Tutorial- what is method overloading in Scala, examples of Scala Method overloading, method overriding in scala, scala override\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Method Overloading with Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Scala Method Overloading Tutorial- what is method overloading in Scala, examples of Scala Method overloading, method overriding in scala, scala override\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-28T06:10:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:45:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Method Overloading with Example - DataFlair","description":"Scala Method Overloading Tutorial- what is method overloading in Scala, examples of Scala Method overloading, method overriding in scala, scala override","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/","og_locale":"en_US","og_type":"article","og_title":"Scala Method Overloading with Example - DataFlair","og_description":"Scala Method Overloading Tutorial- what is method overloading in Scala, examples of Scala Method overloading, method overriding in scala, scala override","og_url":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-28T06:10:37+00:00","article_modified_time":"2021-12-04T04:45:42+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Scala Method Overloading with Example","datePublished":"2018-05-28T06:10:37+00:00","dateModified":"2021-12-04T04:45:42+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/"},"wordCount":335,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg","keywords":["default constructor in scala","Example of Scala Method Overloading","scala final method","Scala Method Overloading","scala operator overloading","scala override","scala this"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/","url":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/","name":"Scala Method Overloading with Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg","datePublished":"2018-05-28T06:10:37+00:00","dateModified":"2021-12-04T04:45:42+00:00","description":"Scala Method Overloading Tutorial- what is method overloading in Scala, examples of Scala Method overloading, method overriding in scala, scala override","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scala-method-overloading\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Method-Overloading-01.jpg","width":1200,"height":628,"caption":"Scala Method Overloading"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scala-method-overloading\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Amazon Developer Tools","item":"https:\/\/data-flair.training\/blogs\/tag\/amazon-developer-tools\/"},{"@type":"ListItem","position":3,"name":"Scala Method Overloading with Example"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=16793"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16793\/revisions"}],"predecessor-version":[{"id":104765,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16793\/revisions\/104765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/16795"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=16793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=16793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=16793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}