Showing posts with label constructors. Show all posts
Showing posts with label constructors. Show all posts

Sunday, 27 January 2019

Groovy: Constructor invocation


There are four ways to invoke the constructor.
a.   Normal Java way using new operator
b.   Using coercion with as keyword
c.    Using coercion in assignment
d.   Using named parameters

For example, take below Employee class, I defined one constructor that takes employee name and id as arguments.

class Employee{
         String name
         int id

         Employee(name, id){
                  this.name = name
                  this.id = id
         }
        
}

Using new operator
Employee emp1 = new Employee("Krishna", 123)

Using coercion with as keyword
Employee emp2 = ["Krishna", 123] as Employee

Using coercion in assignment
Employee emp3 = ["Krishna", 123]

Find the below working application.

HelloWorld.groovy
class Employee{
 String name
 int id

 Employee(name, id){
  this.name = name
  this.id = id
 }
 
}

Employee emp1 = new Employee("Krishna", 123)
Employee emp2 = ["Krishna", 123] as Employee
Employee emp3 = ["Krishna", 123]

void printEmployee(Employee emp){
 println "name: ${emp.name}, id: ${emp.id}"
}

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

Output
name: Krishna, id: 123
name: Krishna, id: 123
name: Krishna, id: 123

Using named parameters
If a class has default constructor, then you can create objects by passing parameters in the form of a map (property/value pairs)


HelloWorld.groovy
class Employee{
 String name
 int id
}

Employee emp1 = new Employee(name : "Krishna", id : 123)
Employee emp2 = new Employee(name : "Krishna")
Employee emp3 = new Employee()


void printEmployee(Employee emp){
 println "name: ${emp.name}, id: ${emp.id}"
}

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

Output
name: Krishna, id: 123
name: Krishna, id: 0
name: null, id: 0



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