T E C h O C E A N H U B

Explaining Overloading and Overriding concept in Kotlin (Chapter-10)

We have learnt about overloading and overriding concept in every other language (specifically the language which is Object-Oriented Language). Overloading concept basically deals with using one method in a multiple-way or if we go for defining it in a much simpler way it can be defined as one man can play multiple roles, he can be a father, son of someone, husband and a brother, this can be a basic example of overloading concept and in the other way round overriding allows inheriting features from the parent class. Both are the basic features required and used in inheritance to.

Above diagram clearly explains the concept now let us go into the programming way of overloading and overriding Overloading in Kotlin way, let us see a simple program of overloading in kotlin.

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

    fun sum(a: Int, b: Int, c:Int) {
        println("Sum($a,$b,$c) = ${a +b+ c}")
    }

}

    //Main function, entry Point of Program
    fun main(args:Array<String>){
        val Obj = MainActivity()
        Obj.sum(10,20)
        Obj.sum(20,10,50)

    }

The above program maintains the overloading of the sum function. It is very similar to other languages(object-oriented languages). We have a function in two ways, in one we have taken two variables, and in the other function( which is using the same name as the above one but the variables are 3, you can take any number of variables but the name of the function should be same and this concept is considered as overloading, so now, let us see the simple program of Overriding in kotlin.

open class MainActivity : AppCompatActivity() {
    //Member function with two Int type Argument
    open fun sum(a: Int, b: Int) {
        println("Sum is  = ${a + b}")
    }
}
class SecondActivity:MainActivity(){
    override fun sum(c: Int, d: Int) {
        println("Sum is = ${c + d}")
    }
}

    //Main function, entry Point of Program
    fun main(args:Array<String>){
        val Obj = MainActivity()
        Obj.sum(10,20)
        val Obj1=SecondActivity()
        Obj1.sum(30, 40)

    }

The programs are similar to other object oriented languages but the difference which appears in syntax itself. which is quite different in Kotlin but semantics always remain the same( which is logic itself).In the above mentioned programs overloading of sum function is easy in kolin but we cannot override directly unless we provide an ‘open’ keyword before the class keyword in kotlin, it applies for method and class both. As discussed before class is final by default, which cannot be overrided directly, we will discuss the “Open” and ‘override‘ keyword in detail in my upcoming post.

Feel free to read the post and comment below.

Below is my android application, and is useful for people who has Google Opinion Rewards to cash out. https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash

If you wanted to create a website, please visit my fiverr gig link below or contact me on support@techoceanhub.com. https://www.fiverr.com/share/EdZ9L7

Copyright ©TechOceanhub All Rights Reserved.