Friday, 29 December 2017

Kotlin: Define private primary constructor

By specifying the 'private' access specifier in the class header, you can define a private primary constructor.

PrivateConDemo.kt

class Employee private constructor(var firstName: String) {

 var lastName: String = ""

 constructor(fName: String, lName: String) : this(fName) {
  this.lastName = lName
 }
}

fun printEmployee(emp: Employee) {
 println("firstName : ${emp.firstName}, lastName : ${emp.lastName}")
}

fun main(args: Array<String>) {
 var emp1 = Employee("Shwetha", "kam")

 printEmployee(emp1)

}

Output

firstName : Shwetha, lastName : kam


Previous                                                 Next                                                 Home

No comments:

Post a Comment