Constructor overloading

When a class contains multiple constructors and each constructor has different parameters, this is called constructor overloading. In Kotlin, each secondary constructor is prefixed with the constructor keyword and each constructor needs to call the primary constructor either directly or indirectly using the this keyword. 

To understand this kind of constructor chaining, let's take a look at another example: 

Write a Product class that contains name, category, price, and quantity. We can create an object of the Product class if we are provided with name and category. In total, there are three ways to create an object:

  • Using the primary constructor with two variables
  • Using the secondary constructor with three variables
  • Using the final constructor with all provided information

The primary constructor contains the name and category parameters. The rest of the properties are initialized with the default values, as follows:

class Product(name: String, category: String)
init {
this.name = name
this.category = category
this.price = 0.0
this.quantity = 0
}

The secondary constructor contains name, category, and price. The this keyword calls the constructor with two parameters (the primary constructor) to initialize the properties:

constructor(name: String, category: String, price: Double) : this(name, category){
this.price = price
}

 The final constructor contains all the properties. The this keyword calls the constructor with three parameters, which eventually calls the primary constructor:

constructor(name: String, category: String, price: Double, quantity: Int) : this(name, category, price){
this.quantity = quantity
}

The following is the class diagram of the Product class:

This class diagram contains four properties and three constructors, as shown in the preceding diagram. Take a look at the following example of constructor-overloading:

class Product(name: String, category: String) {

val name: String
val category: String
var price : Double
var quantity : Int

init {
this.name = name
this.category = category
this.price = 0.0
this.quantity = 0
}

constructor(name: String, category: String, price: Double) : this(name, category){
this.price = price
}

constructor(name: String, category: String, price: Double, quantity: Int) : this(name, category, price){
this.quantity = quantity
}
}

fun main(args: Array<String>) {

val audioPlayer = Product("MP3 Player","Electronics")
println("Product name = ${audioPlayer.name}, Category = ${audioPlayer.category}, Price = ${audioPlayer.price}$ and Available Quantity = ${audioPlayer.quantity}")

val flashRam = Product("Flash Ram","Electronics", 35.0)
println("Product name = ${flashRam.name}, Category = ${flashRam.category}, Price = ${flashRam.price}$ and Available Quantity = ${audioPlayer.quantity}")

val toy = Product("Teddy Bear","Toy", 10.0, 54)
println("Product name = ${toy.name}, Category = ${toy.category}, Price = ${toy.price}$ and Available Quantity = ${toy.quantity}")
}

As we can see in following output, the audioPlayer object is initialized with two necessary values  the product name and the product category:

The rest of the class  properties are initialized with default values. Similarly, the flashRam and toy objects are initialized with the secondary constructors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.11.227