Scala Method Overloading with Example

Free Scala course with real-time projects Start Now!!

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.

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

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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.

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

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

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