ADVERTISEMENT
ADVERTISEMENT

Kotlin program | Example of method overloading

Kotlin | Method Overloading: Here, we are implementing a Kotlin program to demonstrate the example of method overloading.
Submitted by IncludeHelp, on June 03, 2020

Method Overloading

Function overloading or method overloading is the ability to create multiple functions of the same name with different implementations.

Program for method overloading in Kotlin

package com.includehelp

//Declare class
class Operation{
    //Member function with two Int type Argument
    fun sum(a:Int, b:Int){
        println("Sum($a,$b) = ${a+b}")
    }

    //Overloaded Member function 
    //with three Int type Argument
    fun sum(a:Int, b:Int,c:Int){
        println("Sum($a,$b,$c) = ${a+b+c}")
    }

    //Overloaded Member function with 
    //four Int type Argument
    fun sum(a:Int, b:Int,c:Int,d:Int){
        println("Sum($a,$b,$c,$d) = ${a+b+c+d}")
    }
}

//Main function, entry Point of Program
fun main(args:Array<String>){
    //Create Instance of Operation class
    val operation = Operation()

    //Called function with two arguments
    operation.sum(10,20)

    //Called function with three arguments
    operation.sum(a=2,b=3,c=5)

    //Called function with four arguments
    operation.sum(5,8,2,12)
}

Output:

Sum(10,20) = 30
Sum(2,3,5) = 10
Sum(5,8,2,12) = 27
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.