Method Overloading in Scala

27 Jan, 2019
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.
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.
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.
Example:
// 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);    }}
Output:
Sum in function 1 is:14
Sum in function 2 is:73

Example:
// 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);    }}
Output:
Sum in function 1 is:24
Sum in function 2 is:74.61

Example:
// 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);    }}
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


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:
// 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;    }             }
Compile-time Error:
prog.scala:10: error: ambiguous reference to overloaded definition,both method fun in object Main of type (p: Int, q: Int)Doubleand method fun in object Main of type (p: Int, q: Int)Intmatch argument types (Int,Int) and expected result type Anyprintln(“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)Doubleand method fun in object Main of type (p: Int, q: Int)Intmatch argument types (Int,Int) and expected result type Anyprintln(“Sum in function 2 is:” + fun(6, 8) );^prog.scala:22: error: method fun is defined twiceconflicting symbols both originated in file ‘prog.scala’def fun(p:Int, q:Int) : Double = {^three errors found
Scala
Scala-Method
Scala-OOPS
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.