Friday, July 14, 2017

Kotlin OOP: Basic

Welcome to kotlin Series

Kotlin OOP series:
  1. OOP Basic
  2. Constructor
  3. Inheritance
  4. Method Overriding

In this post, we are going to cover object-oriented programming concept with kotlin. In kotlin, all the item is an object. So we are going to create an object name apple. and it has two properties color and shape. Let's create an Apple object class-
private class Apple(color:String,shape:String){
}
now we are going to create two class variable and this variable can nullable. Those are color and shape
var color:String? = null
var shape:String? = null
Now initialize this value in init{}
init {
   this.color = color
   this.shape = shape
}
Not: it just like Java constructor. In java you assign your class variable in the constructor it works just like that. Now we are going to create two methods that return shape and color. Those methods are like Java getter method
fun GetColor():String?{
    return this.color
}

fun GetShape():String?{
    return this.shape
}
oh! we finished the apple class. you check my full apple class code.
private class Apple(color:String,shape:String){

    var color:String? = null
    var shape:String? = null

    init {
        this.color = color
        this.shape = shape
    }

    fun GetColor():String?{
        return this.color
    }

    fun GetShape():String?{
        return this.shape
    }

}
Now time to test this apple class.
Create a main Method-
fun main(args: Array<String>){
}
create a variable name apple and assign to apple class.
val apple =Apple("red","oval")
print apple color and shape-
println("Color: ${apple.GetColor()} Shape: ${apple.GetShape()}")
complete Main method-
fun main(args: Array<String>){
    val apple =Apple("red","oval")

    println("Color: ${apple.GetColor()} Shape: ${apple.GetShape()}")
}
Output:
Color: red Shape: oval
This code like too similar to Java. I try to make it similar to java. But we are learning new language Kotlin. we can make this class is very small through Kotlin concept. we make a new class Apple2. It also has same properties like color and shape.
Let's do that.
code-
class Apple2(var color: String, var shape: String)
our Apple class is complete in just one line. We don't need any greater or setter methods. No extra constructor need.
Let's test this apple class and check the output is same.
fun main(args: Array<String>){
    val apple =Apple2("red","oval")
    println("Color: ${apple.color} Shape: ${apple.shape}")
}
Output
Color: red Shape: oval
So we see that output is same. We don't need to write more code. Another big advantage is that you can define default value when you create the class variable.
See this-
private class Apple2(var color: String = "red",
                     var shape: String = "oval")
on the main method
val deafult = Apple2()
println("Color: ${deafult.color} Shape: ${deafult.shape}")
See we don't pass any value. Those values come from the default value when you assign it.
Output-
Color: red Shape: oval
we can also assign value through variable wise.
see the code-
val appleGreen = Apple2(color = "red",shape = "round")
This is very helpful when you have many variables in your constructor. So you don't check again and again is the value is assigned the correct one.
Check the main method-
val appleGreen = Apple2(color = "red",shape = "round")
println("Color: ${appleGreen.color} Shape: ${appleGreen.shape}")
Output
Color: red Shape: round
Full main Method code-
fun main(args: Array<String>){
    val apple =Apple2("red","oval")
    println("Color: ${apple.color} Shape: ${apple.shape}")

    val deafult = Apple2()
    println("Color: ${deafult.color} Shape: ${deafult.shape}")

    val appleGreen = Apple2(color = "red",shape = "round")
    println("Color: ${appleGreen.color} Shape: ${appleGreen.shape}")

}
and output is
Color: red Shape: oval
Color: red Shape: oval
Color: red Shape: round
That's it today. 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 :