Method Overloading in Scala

Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. Scala can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameter list (i.e. the number of the parameters, the order of the parameters, and data types of the parameters) within the same class.

  • Overloaded methods are differentiated based on the number and type of the parameters passed as an argument to the methods.
  • We can not define more than one method with the same name, Order and the type of the arguments. It would be a compiler error.
  • The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error.

Why do we need Method Overloading ??

If we need to do the same kind of operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.

Different ways of doing overloading methods

Method overloading can be done by changing:



  • The number of parameters in two methods.
  • The data types of the parameters of methods.
  • The Order of the parameters of methods.

By changing the number of parameters.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to demonstrate the function 
// overloading by changing the number 
// of parameters 
class GFG
{
      
    // function 1 with two parameters
    def fun(p:Int, q:Int)
    {
        var Sum = p + q;
        println("Sum in function 1 is:" + Sum);
    }
      
    // function 2 with three parameters
    def fun(p:Int, q:Int, r:Int)
    {
        var Sum = p + q + r;
        println("Sum in function 2 is:" + Sum);
    }
}
object Main 
{
    // Main function
    def main(args: Array[String]) 
    {
          
    // Creating object of GFG class
    var obj = new GFG();
    obj.fun(6, 8);
    obj.fun(5, 10, 58);
    }
}

chevron_right


Output:

Sum in function 1 is:14
Sum in function 2 is:73

By changing the Data types of the parameters

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to demonstrate the function 
// overloading by changing the data types 
// of the parameters 
class GFG
{
      
    // Adding three integer elements
    def fun(p:Int, q:Int, r:Int)
    {
        var Sum = p + q + r;
        println("Sum in function 1 is:"+Sum);
    }
      
    // Adding three double elements
    def fun(p:Double, q:Double, r:Double)
    {
        var Sum = p + q + r;
        println("Sum in function 2 is:"+Sum);
    }
}
object Main 
{
    // Main method
    def main(args: Array[String]) 
    {
          
    // Creating object of GFG class
    var obj = new GFG();
    obj.fun(6, 8, 10);
    obj.fun(5.9, 10.01, 58.7);
    }
}

chevron_right


Output:

Sum in function 1 is:24
Sum in function 2 is:74.61

By changing the Order of the parameters

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to demonstrate the function 
// overloading by changing the 
// order of the parameters 
class GFG
{
      
    // Function 1
    def fun(name:String, No:Int)
    {
        println("Name of the watch company is:" + name);
        println("Total number of watch :" + No);
    }
      
    // Function 2
    def fun(No:Int, name:String )
    {
        println("Name of the watch company is:" + name);
        println("Total number of watch :" + No);
    }
}
object Main 
{
    // Main Function
    def main(args: Array[String])
    {
          
    // Creating object of GFG class
    var obj = new GFG();
    obj.fun("Rolex", 10);
    obj.fun("Omega", 10);
    }
}

chevron_right


Output:

Name of the watch company is:Rolex
Total number of watch :10
Name of the watch company is:Omega
Total number of watch :10

What happens when method signature is same and the return type is different?

The compiler will give error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Only if both methods have different parameter types (so, they have the different signature), then Method overloading is possible.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Example to show error when method signature is same  
// and return type is different. 
object Main {
           
        // Main method
        def main(args: Array[String]) {
     println("Sum in function 1 is:" + fun(6, 8) );
     println("Sum in function 2 is:" + fun(6, 8) );
    }
      
     // function 1 
    def fun(p:Int, q:Int) : Int = {
        var Sum: Int = p + q;
        return Sum;
         
    }
      
 // function 2 
    def fun(p:Int, q:Int) : Double = {
        var Sum: Double = p + q + 3.7;
        return Sum;
    }
     
      
  
}

chevron_right


Compile-time Error:

prog.scala:10: error: ambiguous reference to overloaded definition,
both method fun in object Main of type (p: Int, q: Int)Double
and method fun in object Main of type (p: Int, q: Int)Int
match argument types (Int,Int) and expected result type Any
println(“Sum in function 1 is:” + fun(6, 8) );
^
prog.scala:11: error: ambiguous reference to overloaded definition,
both method fun in object Main of type (p: Int, q: Int)Double
and method fun in object Main of type (p: Int, q: Int)Int
match argument types (Int,Int) and expected result type Any
println(“Sum in function 2 is:” + fun(6, 8) );
^
prog.scala:22: error: method fun is defined twice
conflicting symbols both originated in file ‘prog.scala’
def fun(p:Int, q:Int) : Double = {
^
three errors found



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.