October 2018
Intermediate to advanced
370 pages
9h 15m
English
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:
Read now
Unlock full access