Scala Method Overloading

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 18:12:27 Viewed : 205


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

Search
Sub-Categories
Related Articles

Leave a Comment: