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

Kotlin: Default constructor

If you do not declare any constructor (primary or secondary) in your non-abstract class, kotlin will generate a default primary constructor with no arguments.

What is the access scope of this primary constructor?
The default constructor has public scope by default.


Previous                                                 Next                                                 Home

Thursday, 28 December 2017

Kotlin: Passing default values to primary constructor

You can pass default values to the primary constructor.

Ex:
class Employee(var firstName: String = "Krishna", var lastName: String = "Gurram") {

}

Above snippet defines a primary constructor to the class Employee, with default values.

var emp1 = Employee()
Since we do not pass any arguments to the constructor, firstName and lastName are initialized with default values. 'firstName' is initialized with 'Krishna' and lastName is initialized with 'Gurram'

var emp2 = Employee("Rachit")
Since we passed only first argument, 'firstName' is assigned with value 'Rachit' and 'lastName' is assigned with value 'Gurram'

var emp3 = Employee("Shwetha", "kam")
'firstName' is assigned with value 'Shwetha' and 'lastName' is assigned with value 'Kam'.    

ConstructorDemo.kt

class Employee(var firstName: String = "Krishna", var lastName: String = "Gurram") {

}

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

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

 printEmployee(emp1)
 printEmployee(emp2)
 printEmployee(emp3)

}

Output

firstName : Krishna, lastName : Gurram
firstName : Rachit, lastName : Gurram
firstName : Shwetha, lastName : kam


Previous                                                 Next                                                 Home

Wednesday, 27 December 2017

Kotlin: Secondary Constructors

Kotlin class can have more than one secondary constructors.

How to define secondary constructor?
You should use the keyword ‘constructor’ to define secondary constructor.

Syntax
constructor(arguments){

}

ConstructorDemo.kt
class Employee {
 var firstName: String = ""
 var lastName: String = ""

 constructor(firstName: String) {
  this.firstName = firstName
  this.lastName = firstName
 }

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

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

fun main(args: Array<String>) {
 var emp1 = Employee("Hareesh", "Seeram")
 var emp2 = Employee("Praneeth")

 printEmployee(emp1)
 printEmployee(emp2)

}

Output

firstName : Hareesh, lastName : Seeram
firstName : Praneeth, lastName : Praneeth

Primary constructor must be called from secondary constructor explicitly. What I mean is, if a class has primary constructor, then the secondary constructor must delegate to the primary constructor either directly (or) indirectly using other secondary constructors.
class Employee(var firstName: String) {
         ....
         ....
        
         constructor(fName: String, lName: String) : this(fName) {
                 this.lastName = lName
                 this.id = ""
         }
}

In the above example, Employee class has a primary constructor, We are calling primary constructor using 'this' keyword, while defining secondary constructor.


ConstructorDemo.kt

class Employee(var firstName: String) {
 var lastName: String = ""
 var id: String = ""

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

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

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

fun main(args: Array<String>) {
 var emp1 = Employee("Krishna")
 var emp2 = Employee("Hareesh", "Seeram")
 var emp3 = Employee("Praneeth", "Sai", "123")

 printEmployee(emp1)
 printEmployee(emp2)
 printEmployee(emp3)

}


Output

id : , firstName : Krishna, lastName : 
id : , firstName : Hareesh, lastName : Seeram
id : 123, firstName : Praneeth, lastName : Sai


Previous                                                 Next                                                 Home

Tuesday, 26 December 2017

Is Kotlin class requires body?

Body is optional for kotlin class. Below classes are valid in kotlin.

Ex
class DocService
class Student
Previous                                                 Next                                                 Home

Sunday, 24 December 2017

Kotlin: Constructors

Kotlin classes have a primary constructor and one or more secondary constructors. Constructors are defined using constructor keyword.

Primary Constructor
Primary constructor is defined as part of the class header.

Ex
class Employee constructor(fName: String, lName: String) {
         ....
         ....
}

Employee.kt
class Employee constructor(fName: String, lName: String) {
 init{
  println("fName : $fName, lName : $lName")
 }
}

Main.kt
fun main(args: Array<String>) {
 var emp1 = Employee("Hareesh", "Seeram")
}
Output
fName : Hareesh, lName : Seeram

init{} is equivalent to the initializer block of java, it is used to place the object initialization code. This block is called on every object instantiation.

You can omit the constructor keyword while defining primary constructor, if the constructor do not have any modifier (or) annotation associated with it.


Employee.kt
class Employee (fName: String, lName: String) {
 init{
  println("fName : $fName, lName : $lName")
 }
}

Defining member variables as part of primary constructor
If you add the keyword ‘var’/’val’ to the type parameter in primary constructor, it creates a field in class.


Main.kt
class Employee(var firstName: String, var lastName: String) {

}

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

 println("firstName : ${emp1.firstName}, lastName : ${emp1.lastName}")
}

Output

firstName : Hareesh, lastName : Seeram

In the above snippet, I defined firstName and lastName fields as part of primary constructor




Previous                                                 Next                                                 Home

Kotlin: Body of the class is optional

In kotlin, you no need to define the body to a class, it is optional.

Employee.kt
class Employee

As you see, I defined Employee class with no body.

Previous                                                 Next                                                 Home