Full width home advertisement

Post Page Advertisement [Top]

We are going to learn about Kotlin Classes, primary constructor, secondary constructor, initialization etc in a very good approach by comparing Java.

In Kotlin, class keyword is use to create a Class (as same as Java)

Example:

HelloWorld.kt

class HelloWorld /* In kotlin is class body is empty, then {} is not needed. */


And in Java

HelloWorld.java

class HelloWorld { }


Now we add some class fields(i.e. class variable to our class and see the difference)



HelloWorld.kt

class HelloWorld{
     private var name:String  /* This is class field and confirm that it never be null */
     
     /*Below block is primary constructor initialization block*/
     init{
          name = "Test"
     }
}

And in Java

class HelloWorld{
     private String name; /* Here name may be null */
     
     public HelloWorld(){
          name = "Test";
     }
}





Now we will initialize class fields with primary constructor, primary constructor means the 1st constructor.



class HelloWorld(private var name:String)


And in Java

class HelloWorld{
     private String name;
     
     public HelloWorld(String name){
          this.name = name;
     }
}


Here you see the code difference, In kotlin every field which will initialize by constructor will be move in class i.e. class Test(var field1:String,var field2:Int,var field3:Any)

Note: If you want to make class field private, use private modifier,.
If you remove the word "var", field will only available in init method



Constructor overloading in Kotlin vs Java

In Java

class HelloWorld{
     public int id;
     public String name;
     public int year;
     
     public HelloWorld(int id){
          this.id = id;
          this.name = "No Name";
          this.year = 0;
     }

     public HelloWorld(int id, String name){
          this.id = id;
          this.name = name;
          this.year = 0;
     }

     public HelloWorld(int id, String name, int year){
          this.id = id;
          this.name = name;
          this.year = year;
     }
}

In Kotlin

class HelloWorld(var id:Int, var name:String = "No Name", var year:Int=0)


You can use Kotlin class like this:

var c1 = HelloWorld(id=2, year=5)
var c2 = HelloWorld(10);
var c3 = HelloWorld(10, "Hello", 5)
var c4 = HelloWorld(year=5, id=2, name="Test")

As you see above, name and year param are optional, if you don't provide values to this field, it will use the default values. These are called optional parameter and can be used in class as well as in functions(method)



Secondary Constructor in Kotlin

In Kotlin, there is secondary constructor too. If the above way is not fit for your code, you can use secondary constructor.

class HelloWorld(var id: Int){

     constructor(var world: HelloWorld): this(world.id){

     }
}


Here, constructor word is use to create a secondary constructor.
In the above code, there is two consturctor:
     1.Primary Constructor (needs id)
     2. Secondary Constructor (need HelloWorld object)

In Secondary constructor, we use :this(world.id) , it means we must call primary constructor first. If you don't want to call primary constructor, don't make primary constructor.


1 comment:

  1. Thanks you. This kotlin tutorial for constructor help me a lot.

    ReplyDelete

Bottom Ad [Post Page]

| Designed by Colorlib