user9865139
user9865139

Reputation:

how do i implement java like constructors in kotlin

I have been trying to add a secondary constructor to my heelo world program in kotlin. The following is my code. (I used online kotlin ide https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt)

 class Test(var name:String){

        constructor(age:Int,data:Int){
            println("$age $data")
        }
        fun display(){
        println("hello world $name")
        }
    }
    fun main(args: Array<String>) {
        var t=Test("john")
        var t1=Test(10,25)
        t.display()
    }

But it shows me the following error

Simplest version.kt

    Error:(8, 4) Primary constructor call expected
    Warning:(18, 8) Variable 't1' is never used

After some googling i resolved the error. changing the second parameter of secondary constructor to string type and adding :this(data) solved the problem .The following is the resolved code

class Test(var name:String){

    constructor(age:Int,data:String):this(data){
        println("$age $data")
    }
    fun display(){
    println("hello world $name")
    }
}
fun main(args: Array<String>) {
    var t=Test("john")
    var t1=Test(10,"25")
    t.display()
}

But the problem is i want to pass two integer values to the secondary constructor.I tried the following,but it gave this error

class Test(var name:String){

    constructor(age:Int,data:Int):this(data){
        println("$age $data")
    }
    fun display(){
    println("hello world $name")
    }
}
fun main(args: Array<String>) {
    var t=Test("john")
    var t1=Test(10,25)
    t.display()
}

but it gave this error

Simplest version.kt

    Error:(8, 39) Type mismatch: inferred type is Int but String was expected
    Warning:(17, 8) Variable 't1' is never used

How do i achieve this?. How can I pass two integer variable to the secondary constructor?

Upvotes: 0

Views: 444

Answers (2)

tompee
tompee

Reputation: 1408

In kotlin, you have to call the primary constructor from the additional constructor. From what I understand, you want to have 2 constructors of totally different input. There are plenty of ways to achieve this. First is defining a default value from your additional constructors.

class Test(var name:String) {

    constructor(age:Int,data:Int):this("default name here"){
        println("$age $data")
    }

    fun display(){
        println("hello world $name")
    }
}

The above code calls the the primary constructor with a default string as parameter, since you are not planning on accepting this name parameter on your additional constructor.

Another way is optional parameters. You can define your primary constructor argument as optional parameter. In this way you do not have to provide a value in the primary constructor call.

class Test(var name:String = "default value here") {

    constructor(age:Int,data:Int):this(){
        println("$age $data")
    }

    fun display(){
        println("hello world $name")
    }
}

There is one catch with this 2nd approach though. You now have (effectively) 3 constructors, 1 of which is empty.

Upvotes: 1

ESala
ESala

Reputation: 7058

@tompee's answer is better. But this is another option:

class Test(var name: String) {

    constructor(age: Int, data: Int, name: String) : this(name) {
        println("$age $data $name")
    }

    fun display() { println("hello world $name") }

}

fun main(args: Array<String>) {
    var t = Test("john")
    var t1 = Test(10, 25, "peter")
    t.display()
}

Upvotes: 0

Related Questions