Scala Method Overloading

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 12:42:27 Viewed : 534


Scala Method Overloading

Scala provides method overloading feature which allows us to define methods of same name but having different parameters or data types 

Example:

Following example illustrates about Scala method overloading

Save the file as −  Arithmetic.scala 

Arithmetic.scala 

 

 package runnerdev 

class Arithmetic {

  def add(x: Int, y: Int) {

    var sum = x + y

    println(sum)

  }

  def add(a: Int, b: Int, c: Int) {

    var sum = a + b + c

    println(sum)

  }

} 

object MethodOverload {

  def main(args: Array[String]) {

    var arObj = new Arithmetic();

    arObj.add(100100);

    arObj.add(1010010);

  }

}

 compile and run the above example as follows 

scala> scalac Arithmetic.scala

scala> scala Arithmetic

Output

200

120

Method overloading in Scala allows you to define multiple methods with the same name within the same class or object, but with different parameter lists. The methods must have different numbers or types of parameters to be considered overloaded. This is a form of polymorphism that provides flexibility when calling methods with various argument combinations.

Her is an example of method overloading in Scala:

scala
class Calculator { // Method 1: Add two integers def add(x: Int, y: Int): Int = { x + y } // Method 2: Add three integers def add(x: Int, y: Int, z: Int): Int = { x + y + z } // Method 3: Add two doubles def add(x: Double, y: Double): Double = { x + y } } object MethodOverloadingExample { def main(args: Array[String]): Unit = { val calc = new Calculator() println(calc.add(1, 2)) // Calls Method 1, output: 3 println(calc.add(1, 2, 3)) // Calls Method 2, output: 6 println(calc.add(1.5, 2.5)) // Calls Method 3, output: 4.0 } }

In this example:

  1. Calculator is a class with three overloaded add methods:

    • add(x: Int, y: Int) adds two integers.
    • add(x: Int, y: Int, z: Int) adds three integers.
    • add(x: Double, y: Double) adds two doubles.
  2. In the MethodOverloadingExample object, we create an instance of Calculator and demonstrate calling each of the overloaded add methods with different argument combinations.

The key points to note about method overloading in Scala are:

  • The return type can be the same or different for overloaded methods.
  • The compiler selects the appropriate method based on the number and types of arguments provided when the method is called.

This flexibility in method overloading allows you to create cleaner and more intuitive APIs by providing multiple ways to call a method depending on the context and needs of the code.

Here are examples of method overloading in Scala with explanations and their respective outputs:

Example 1: Method Overloading with Different Parameter Types

scala
class Calculator { // Method 1: Add two integers def add(x: Int, y: Int): Int = { x + y } // Method 2: Add two doubles def add(x: Double, y: Double): Double = { x + y } } object MethodOverloadingExample { def main(args: Array[String]): Unit = { val calc = new Calculator() val result1 = calc.add(2, 3) // Calls Method 1 val result2 = calc.add(2.5, 3.5) // Calls Method 2 println(s"Result 1: $result1") // Output: Result 1: 5 println(s"Result 2: $result2") // Output: Result 2: 6.0 } }

In this example, we have a Calculator class with two overloaded add methods. The first method adds two integers, and the second method adds two doubles. Depending on the argument types passed when calling the add method, the appropriate overloaded method is invoked.

Example 2: Method Overloading with Different Number of Parameters

scala
class MathOperations { // Method 1: Add two integers def add(x: Int, y: Int): Int = { x + y } // Method 2: Add three integers def add(x: Int, y: Int, z: Int): Int = { x + y + z } } object MethodOverloadingExample { def main(args: Array[String]): Unit = { val mathOps = new MathOperations() val result1 = mathOps.add(2, 3) // Calls Method 1 val result2 = mathOps.add(2, 3, 4) // Calls Method 2 println(s"Result 1: $result1") // Output: Result 1: 5 println(s"Result 2: $result2") // Output: Result 2: 9 } }

In this example, we have a MathOperations class with two overloaded add methods. The first method adds two integers, and the second method adds three integers. The appropriate method is called based on the number of arguments provided.

Method overloading allows you to create multiple methods with the same name but different parameter lists, making your code more flexible and intuitive when dealing with different argument types or numbers of arguments.

Search
Related Articles

Leave a Comment: