Kotlin Function Overloading Tutorial

In this section, we will learn what the function overloading is and how it works in Kotlin.

What is Function Overloading in Kotlin?

Function overloading is the process of using the name of a function in order to create multiple different functions!

Basically, when creating multiple functions with the same name, this process is called function overloading.

The main reason of why we overload a function is because we have a task (like an arithmetic operation) and want to run that on multiple different types of values!

For example, let’s say we have a function named multiply that takes two parameters of type Integer, multiplies those values and returns the result.

Now if we want to create the same functionality for other data types (for example double or float values) we can still use the same name multiply to create another function but this time with parameters of type Float or Double.

Note that we only use the same name for functions! But the rest of signature of a function must be different if we want to correctly overload a function without confusing the Kotlin compiler.

How to perform Function overloading in Kotlin?

As mentioned before, when overloading a function, other than the name of a function, you can’t use the same signature to create another function! That way, you’re overriding a function and this operation is not allowed in Kotlin except for when it comes to inheritance, which we will talk about it in the Kotlin inheritance and function overriding section.

In short, there are two ways to overload a function in Kotlin:

1- Function overloading via number of parameters

This method is basically saying you can use the same name for multiple functions as long as each function has a different number of parameters in it.

For example, if the first function has 1 parameter, then the next one must have 0 or over one parameter in it.

Example: overloading a function using number of parameters in Kotlin

fun main(){

    sayHello("John","Doe")

    sayHello("Elon")

}

fun sayHello (name:String) = println("Hello ${name} ")

fun sayHello (name:String, lastName:String) = println("Hello ${name} ${lastName}")

Output:

Hello John Doe

Hello Elon

Here there are two functions with the name sayHello. The first one has only one parameter and the second one contains two parameters.

So using the number of parameters in each function, we could correctly overload the sayHello function and so now when we call the function with just one argument, Kotlin knows which function to invoke (the one with just one parameter) and also in can easily find the one with two parameters whenever we call the function and pass two arguments to it.

2- Function overloading via data type of parameters

The next method of overloading a function is by setting the same number of parameters, but changing the data type of at least one of those parameters to something different.

This way, Kotlin can differentiate between functions by looking at the data type of the parameters of the functions.

Example: overloading a function using the data type of parameters in Kotlin

fun main(){

    sayHello("John","Doe")

    sayHello("Elon", 50)

}

fun sayHello (name:String, age:Int ) = println("Hello ${name}, your age is ${age} ")

fun sayHello (name:String, lastName:String) = println("Hello ${name} ${lastName}")

Output:

Hello John Doe

Hello Elon, your age is 50

In this example, the sayHello function is overloaded. Both functions have the same number of parameters, but the second parameter for the first function is of type Int while the type of the second parameter in the second function is of type String. Now using this difference, Kotlin can easily differentiate between the functions and find the correct one when we call the sayHello function and pass arguments to it.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies