Constructing a class with a constructor

When an object is created, all class members have to have an initial value. We can either initialize a property directly or the initial values can be provided by a constructor. A constructor is a special type of function that is used to initialize the properties of the class. 

A constructor is invoked when the object is created, or, more specifically, when the space is allocated in the memory. This special function has two important characteristics:

  • Constructors can be declared with the constructor keyword
  • Constructors don't contain any return types, not even Units

In this section, we will learn about all of the different kinds of constructors that are provided by Kotlin. Let's start with the default constructor.

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

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