Scala Method Overloading with Example

1. Objective

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’s begin Scala Method Overloading.

Scala Method Overloading

Scala Method Overloading

2. What is Scala Method Overloading?

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, data types, or both. This makes for optimized code.
Let’s discuss Scala String Method with Syntax and Method

Scala Quiz

3. Example of Overloading Method in Scala

Here, we are going to understand Scala Method Overloading with examples.

Example – 1 

Let’s take an example with a different number of parameters.

scala> class sayHello{
     | def hello(){
     | println("Hello, user")
     | }
     | def hello(admin:String){
     | println("Hello, "+admin)
     | }
     | def hello(admin:String,guest:String){
     | println("Hello, "+admin+", Hello "+guest)
     | }
     | }
defined class sayHello
scala> var h=new sayHello()
h: sayHello = sayHello@406ad6d5

We defined a new object ‘h’ 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). While one version addresses the admin, another address both- the admin and the guest. The default version just says hello to user.
Read about Scala Access Modifiers: Public, Private and Protected Members

scala> h.hello()

Hello, user

scala> h.hello("Ayushi")

Hello, Ayushi

scala> h.hello("Ayushi","Megha")

Hello, Ayushi, Hello Megha

Example – 2

Now, let’s 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.

scala> class Calculator{
     | def sum(a:Int,b:Int){
     | var sum=a+b
     | println(sum)
     | }
     | def sum(a:Double,b:Double){
     | var sum=a+b
     | println(sum)
     | }
     | }
defined class Calculator
scala> var c=new Calculator()
c: Calculator = Calculator@1d3e5a05
scala> c.sum(1,2)

3

scala> c.sum(1.1,2.2)

3.3000000000000003
In this example, we have two versions of the method ‘sum’- one that adds two integers, and the other that adds two floats. It then prints out the sum.
Let’s Learn Scala Environment Setup and Get Started with an IDE
So, this was all about Scala Method Overloading tutorial. Hope you like our explanation.

4. Conclusion

Hence, we come to the end of Scala Method Overloading tutorial, we finally knew method overloading in Scala with example. Do let us know what you think in the comments.
Related Article – Scala String Interpolation 
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.