Function overloading

Function-overloading is a feature where a class has more than one function with the same names. Each function is uniquely identified by its parameters. In the following example, a Person class contains two functions with the same name  fun greet(), which doesn't contain any parameters, and fun greet(message : String), which contains one string parameter. Kotlin can determine the function call by the parameters provided:

class Person (val name: String, var age : Int , var height : Double){

fun speak(){
println("My name is $name , i am $age years old and
I am
$height feet tall")
}

fun greet(message : String){
println("Hi I am $name.... $message")
}

fun greet(){
println("Hi I am $name.... Nice to meet you!!!")
}
}

fun main(args: Array<String>) {
val abid = Person("Abid", 40, 6.0)
abid.greet()
abid.greet("How are you doing, it is a pleasure to have you here:")
}

As we can see, the Kotlin compiler automatically figures out which greet function to call:

Function overloading is a very powerful feature, especially where different activities of the same type that take different parameter types are performed. To understand the importance of function-overloading, declare a class called Calculator, which can perform different arithmetic operations, such as adding, multiplying, and subtracting. The following class contains four functions with the same name. The first add function takes two parameters of the Integer type, while the second add function takes two parameters of the Double type, and so on:

class Calculator{

fun add(v1 : Int, v2 : Int) = v1 + v2
fun add(v1 : Double, v2 : Double) = v1 + v2
fun add(v1 : Float, v2 : Float) = v1 + v2
fun add(v1 : Int, v2 : Int, v3 : Int) = v1 + v2 + v3

fun max(v1 : Int, v2 : Int) = if (v1 >= v2) {
v1
} else {
v2
}

fun max(v1 : Double, v2 : Double) = if (v1 >= v2) {
v1
} else {
v2
}
}

fun main(args: Array<String>) {
val calc = Calculator()
println(calc.add(2,2))
println(calc.add(3.0,3.0))
println(calc.add(4.0f,4.0f))
println("MAX "+ calc.max(3,4))
}

Whenever a function is called, the Kotlin compiler compares the function name and the number of parameters along with the parameter type to invoke the correct function:

In our example, add(2,2) calls the add function with integer parameters, add(3.0,3.0) calls the add function with double parameters, and so on. The Kotlin compiler performs all the verification behind the scenes and the programmer doesn't need to do anything.

The overloaded function is being distinguished by its name and its parameters list, not by its return type. We cannot overload a function by changing its return type. The following code is not a valid example of function-overloading even they have different return types. The reason why this was unsuccessful is that when the add(2,2) function is called, the Kotlin compiler does not have enough information to choose which function to invoke:    

fun add(v1 : Int, v2 : Int) : Int {
return v1 + v2
}

fun add(v1 : Int, v2 : Int) : Double {
return v1.toDouble() + v2.toDouble()
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.34