Saturday, July 15, 2017

Kotlin OOP: method overriding

Welcome to kotlin Series



Kotlin OOP series:
  1. OOP Basic
  2. Constructor
  3. Inheritance
  4. Method Overriding
This is the fourth post in this Kotlin OOP series. In this series, we are going to cover a very important topic that is method overriding. This uses in the android app development is huge. First, we create two classes and we use one class method for other class. But we already know it how we can do that. we can do this? In the third part of this series. But in this tutorial, we are going to do some advance. We modify first class method in the second class.

Let's start.
Create a class name Math3 and also create 2 two class. Like previous Math class.

See the code-
class Math3 {

    fun add(a:Int, b:Int):Int{
        return a+b
    }

    fun sub(a:Int, b:Int):Int{
        return a-b
    }
}
Now create a second class name Math4 and it also has two methods.(like previous second math class). But take an extra variable and we use this variable in the override methods.
See the code-
class Math4(val extra:Int) {

    fun mul(a:Int, b:Int):Int{
        return a*b
    }

    fun div(a:Int, b:Int):Int{return a/b}
}
two class creation complete. Now we do our main job. Before inheriting one class to another make class as open and methods also.

See the code-
open class Math3 {

    open fun add(a:Int, b:Int):Int{
        return a+b
    }

    open fun sub(a:Int, b:Int):Int{
        return a-b
    }
}
Now inherit the first class, to second class.
class Math4(val extra:Int) : Math3(){
     //some code
}
we override two methods from first math class (Math3.kt). in add and sub-method we just add our extra value.

See the code-
add method
override fun add(a:Int, b:Int):Int{
    return a+b+extra
}
submethod-
override fun sub(a:Int, b:Int):Int{
        return a-b+extra
}
That it we finish. Now we run this code in the Main method.

see full code of Math4.kt
class Math4(val extra:Int) : Math3(){

    override fun add(a:Int, b:Int):Int{
        return a+b+extra
    }

    override fun sub(a:Int, b:Int):Int{
        return a-b+extra
    }

    fun mul(a:Int, b:Int):Int{
        return a*b
    }

    fun div(a:Int, b:Int):Int{return a/b}
}
on the Main method, we first create an instance of the Math3 class and use Math3 methods.
fun main(args: Array<String>){

    val math = Math3()
    println("add: ${math.add(20,21)}")
    println("add: ${math.sub(22,21)}")
}
Output-
add: 41
Sub: 1
Now time for Math4. use 4 methods and pass value 10.
val math2 = Math4(10)
println("add: ${math2.add(20,21)}")
println("sub: ${math2.sub(22,21)}")

println("mul: ${math2.mul(20,21)}")
println("div: ${math2.div(22,21)}")
Output-
add: 51
sub: 11
mul: 420
div: 1
Now compare two those output. you see 10 different in the result of add and sub.
Main Method-
fun main(args: Array<String>){

    val math = Math3()
    println("add: ${math.add(20,21)}")
    println("Sub: ${math.sub(22,21)}")

    val math2 = Math4(10)
    println("add: ${math2.add(20,21)}")
    println("sub: ${math2.sub(22,21)}")

    println("mul: ${math2.mul(20,21)}")
    println("div: ${math2.div(22,21)}")

}
Output-
add: 41
Sub: 1
add: 51
sub: 11
mul: 420
div: 1
That's it.
Now we learn how we can modify one class methods in other class.

Thank you for reading this post

Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :