Showing posts with label primary constructor. Show all posts
Showing posts with label primary constructor. Show all posts

Thursday, 4 January 2018

Kotlin: Inheritance

Inheritance is the concept of re usability. Object of one class can get the properties and methods of object of another class by using inheritance.

By using inheritance, you can create new class by extending the existing class. The new class is called derived class and the existing class is called base class.



Example
open class BaseClass{

}

class DerivedClass : BaseClass{

}


By default, all the classes in kotlin are final. To make these class inheritable, you should use the keyword ‘open’.


Let’s see inheritance feature by an example. In every organization, there are two kinds of employees.
a.   Permanent Employees
b.   Contract Employees

Both the permanent and contract employees share properties like firstName, lastName and id. Where as contract employees has separate payroll company. We can place these common properties (firstName, lastName and id) in separate class Employee and make the classes PermanentEmployee and ContractEmployee inherit these properties from the common base class Employee.

open class Employee {

}

class PermanentEmployee : Employee {

}

class ContractEmployee : Employee {

}

Find the below working application.


Inheritance.kt
open class Employee {
 var firstName: String = ""
 var lastName: String = ""
 var id: String = ""

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

 open fun getEmployee(): String {
  return "firstName : $firstName, lastName: $lastName, id: $id"
 }

}

class PermanentEmployee : Employee {
 var healthInsuranceNumber = ""
 var salary: Double = 0.0

 constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id) {
  this.healthInsuranceNumber = healtInsuranceNumber
  this.salary = salary
 }

 override fun getEmployee(): String {
  return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
 }

}

class ContractEmployee : Employee {
 var payRollCompany = "ABC"

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

 override fun getEmployee(): String {
  return super.getEmployee() + ", payRollCompany: $payRollCompany"
 }
}


fun main(args: Array<String>) {
 var emp = PermanentEmployee("krishna", "Gurram", "123", "health3425", 56789.01)

 println(emp.getEmployee())
}


Output
firstName : krishna, lastName: Gurram, id: 123, healthInsuranceNumber: health3425, salary: 56789.01

constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id)
If super class don’t have default constructor, then all the sub class constructors must call the super class constructor explicitly.

By default, all the functions and classes are not inheritable (final), to make them inheritable, you should use the keyword ‘open’.

open class Employee {

         open fun getEmployee(): String {
                 return "firstName : $firstName, lastName: $lastName, id: $id"
         }

}

To override a function of super class, you should use override keyword.

class PermanentEmployee : Employee {

         override fun getEmployee(): String {
                 return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
         }

}

Important Points
a.   If the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.


b.   If the class has no primary constructor, then each secondary constructor has to initialize the base type using the super keyword, or to delegate to another constructor which does that. Note that in this case different secondary constructors can call different constructors of the base type:


Previous                                                 Next                                                 Home

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

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