Google News
logo
Scala - Interview Questions
What is method overloading in Scala?
Scala provides method overloading feature which allows us to define methods of the same name but having different parameters or data types. It helps to optimize code. You can achieve method overloading either by using different parameter list or different types of parameters.

Example :
class Arithmetic{  
    def add(a:Int, b:Int){  
        var sum = a+b  
        println(sum)  
    }  
    def add(a:Int, b:Int, c:Int){  
        var sum = a+b+c  
        println(sum)  
    }  
}  
 
object MainObject{  
    def main(args:Array[String]){  
        var a  = new Arithmetic();  
        a.add(10,10);  
        a.add(10,10,10);  
    }  
}  ​
Advertisement