-1
class student1(firstName : String, lastName : String){
    var id : Int = -1
    val firstName = firstName
    val lastName = lastName

    init {
       println("initialized")
    }

    constructor(firstName : String, lastName : String, extraParam : Int) : this(firstName, lastName){    
        this.id = extraParam
    }
    
    fun callme(){
        print(firstName + lastName)
    }
}


class student2(firstName : String, lastName : String){
    val firstName = firstName
    val lastName = lastName
    
    fun callme() {
        print(firstName + lastName)
    }
}

fun main() {

  val p1 = student1("shubham", "sharma")   
  println(p1.firstName)
  println(p1.lastName)
  println(p1.callme())
  val p2 = student1("shubham", "sharma")   
  println(p2.firstName)
  println(p2.lastName)
  println(p2.callme())

}

here in both the class, the output is the same with the same parameter then why we need to use the secondary constructor? What is the main difference between these two class please let me know with one example. will be appreciated!

2 Answers2

0

the first one has two constructors that get an additional variable but does not use it, that's why you don't see the difference. the student1 class has an optional id which is -1 by default. if you don't use it somewhere else you must remove it. actually, we don't create classes like this in kotlin you can move the var, val keywords in the constructor:

class Student1(val firstName : String, val lastName : String) {

    var id = -1
    
    init {
        println("initialized")
    }

    constructor(firstName : String, lastName : String, id: Int = -1) : this(firstName, lastName) {
        this.id = id
    }

    fun callme() {
        print(firstName + lastName)
    }
}

you can even make this shorter with default arguments and remove the secondary constructor and make id a val (if you don't want to change it):

class Student1(val firstName : String, val lastName : String, val id: Int = -1) {
    
    init { println("initialized") }

    fun callme() { print(firstName + lastName) }
}
Mohsen
  • 1,246
  • 9
  • 22
  • Yeah! I had just maid it to see what the difference is actually. you said in the last even more shorted code is what I want to use but i thought why the secondary cons is then in use even if we can explicitly do the things. Thanks for this polite response! – shubham KUMAR Jan 11 '21 at 13:32
  • you're welcome. if the answer helped you you can mark it as the accepted answer :D – Mohsen Jan 11 '21 at 13:35
  • Could you please give a example where we can see the use of secondary constructor in best use? – shubham KUMAR Jan 11 '21 at 13:40
  • see this https://kotlinlang.org/docs/reference/classes.html#constructors – Mohsen Jan 11 '21 at 13:52
  • Yeah I had gone through this but didn't get the main moto for this constrictor! Sry for that. – shubham KUMAR Jan 11 '21 at 13:55
0

There is no difference because you use only student1 !

val p2 = student1("shubham", "sharma")

instead of

val p2 = student2("shubham", "sharma")  

To see a difference, you have to make it visible :

class student1 ...
    
    fun callme(){
        print(firstName + lastName + id)
    }

Then use student1 with the secondary constructor :

  val p3 = student1("shubham", "sharma", 2021)   
  println(p3.firstName)
  println(p3.lastName)
  println(p3.callme())

will output

shubhamsharma2021kotlin.Unit
BernardK
  • 3,674
  • 2
  • 15
  • 10
  • Yeah got it. Thanks a lot for your time. But could you give a example where we can see the main role of secondary constrictor? – shubham KUMAR Jan 11 '21 at 13:34
  • Using p1 and the primary constructor, you accept the default value for id, using p3 and the secondary constructor, you give an explicit value to `extraParam` -> `id`. Taken from "Programming Kotlin (The Big Nerd Ranch Guide)" : _When you specify a primary constructor, you say, "These parameters are required for any instance of this class." When you specify a secondary constructor, you provide alternative ways to construct the class (while still meeting the requirements of the primary constructor)._ – BernardK Jan 11 '21 at 14:16
  • Yeah it makes a sense! Here the primary constructors are always be there to any instances of the class and if we use secondary constructors we can explicitly give a extra property along with the delegation of primary constructors isn't it? – shubham KUMAR Jan 11 '21 at 14:35
  • @shubhamKUMAR Exact ! – BernardK Jan 11 '21 at 14:37