Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - FUNCTION OVERLOADING


Function Overloading means, when two or more methods have the same name but different parameters.


Below are two types of difference in parameters that Kotlin considers for method overloading :

  1. The number of parameters are different in two or more methods.

  2. The number of parameters are same in two or more methods but have different datatypes.

Now, let's see the first way of operator Overloading.

  1. The number of parameters are different in two or more methods.

    1. Say there is an 'add' method with three parameters :

      fun add(firstNumber: Int, secondNumber: Int, thirdNumber: Int): Int

    2. Also there is an 'add' method with two parameters :

      fun add(firstNumber: Int, secondNumber: Int): Int

    3. Also, there is an 'add' method with just one parameter :

      fun add(firstNumber: Int): Int


    All the above three methods are said to be overloaded methods and java would treat them as separate methods.

    So, let us try adding 2 numbers with 'add' method. Then try adding 3 numbers with the overloaded 'add' method.

    Example :



    fun main(){
    
    	var firstNum: Int = 5
    	var secondNum: Int = 8
    	var thirdNum: Int = 2
    	var addedResult1: Int
    	var addedResult2: Int
    
    	addedResult1 = add(firstNum,secondNum)
    
    	println("The added value of two numbers is : "+addedResult1)
    
    	addedResult2 = add(firstNum,secondNum,thirdNum)
    
    	println("The added value of three numbers is : "+addedResult2);
    }
    
    //Add method starts here.
    fun add(firstNumber: Int, secondNumber: Int): Int {
    
    	var result: Int
    	result = firstNumber + secondNumber
    	return result
    }
    
    //Overloaded Add method starts here.
    fun add(firstNumber: Int, secondNumber: Int, thirdNumber: Int): Int {
    
    	var result: Int
    	result = firstNumber + secondNumber + thirdNumber
    	return result
    }
    


    Output :



      The added value of two numbers is : 13
      The added value of three numbers is : 15


    In the main method, at first, we have called the 'add' method with two parameters :

    addedResult1 = add(firstNum,secondNum)


    Then we have called the 'add' method with three parameters :

    addedResult2 = add(firstNum,secondNum,thirdNum)


    And Kotlin treated both the methods as different methods, since they were overloaded.

    Next, let us look at the second way of Operator Overloading.

  2. The number of parameters are same in two or more methods but have different datatypes.

    1. Say there is an 'add' method with two parameters :

      fun add(firstNumber: Float, secondNumber: Float): Float


    2. Also there is an 'add' method with two parameters :

      fun add(firstNumber: Int, secondNumber: Int): Int


    Since, the datatype of the parameters in two methods do not match(i.e. Float and Int). The methods are said to be overloaded and Kotlin would treat them as separate methods.

    Example :



    fun main(){
    
    	var firstNum: Int = 5
    	var secondNum: Int = 8
    
    	var firstNumFloat: Float = 2.4F
    	var secondNumFloat: Float = 9.5F
    
    	var addedResult1: Int
    	var addedResult2: Float
    
    	addedResult1 = add(firstNum,secondNum)
    
    	println("The added value of two numbers : "+addedResult1)
    
    	addedResult2 = add(firstNumFloat,secondNumFloat)
    
    	println("The added value of decimal numbers : "+addedResult2);
    }
    
    //Add method starts here.
    fun add(firstNumber: Int, secondNumber: Int): Int {
    
    	var result: Int
    	result = firstNumber + secondNumber
    	return result
    }
    
    //Overloaded Add method starts here.
    fun add(firstNumber: Float, secondNumber: Float): Float {
    
    	var result: Float
    	result = firstNumber + secondNumber
    	return result
    }
    


    Output :



      The added value of two numbers : 13
      The added value of decimal numbers : 11.9


    So, in the above code we have two methods with the same name but parameters are of different Data Types.

    fun add(firstNumber: Int, secondNumber: Int): Int


    And

    fun add(firstNumber: Float, secondNumber: Float): Float


    So, when the method is called with two Integer values,

    addedResult1 = add(firstNum,secondNum)


    The method with two Int parameteres is called,

    fun add(firstNumber: Int, secondNumber: Int): Int


    Similarly, when the method is called with two Float values,

    addedResult2 = add(firstNumFloat,secondNumFloat)


    The method with two Float parameteres is called,

    fun add(firstNumber: Float, secondNumber: Float): Float