https://pl.kotl.in/Lzmw3BYZw?theme=darcula
Although this class doesn’t have a body yet, we can still instantiate it using a default constructor.
https://pl.kotl.in/cWJMkONZR?theme=darcula
As you can observe in the snippet above, we didn’t use the `new` keyword to instantiate this class — as is usual in other programming languages. `new` is not a keyword in Kotlin. This makes our source code more concise when creating a class instance. But note that the instantiation of a Kotlin class in Java will require the `new` keyword.
Properties
Kotlin allows you to declare properties inside Kotlin classes in the same way you declare any other variable. These properties can either be mutable (declared using `var`) or immutable (declared using `val`).
Let’s see this in action!!
https://pl.kotl.in/rXEfYw6L4?theme=darcula
You can get or set the properties of an object using the dot(`.`) notation like this:
https://pl.kotl.in/qWx7KvlRX?theme=darcula
Getter and Setter
Have you ever wondered what happens under the hood while using val and var? Kotlin internally generates a default getter and setter for mutable properties(var), and only a getter for read-only properties(val).
Whenever you access or modify a property using the dot(`.`) notation Kotlin calls these getters and setters internally.
Here is what the `MovieCast` class looks like with the default getters and setters –
https://pl.kotl.in/gcU29mNqH?theme=darcula
There are two new identifiers introduced in all the getter and setter methods — `field` and `value`. The purpose of value is that it serves as the name of the setter parameter, this is the default convention in Kotlin, however, you can use whatever name suits you best. On the other hand, field which is also known as backing field helps you refer to the property inside the getter and setter methods. This is necessary because if you implement the property directly inside the getter or setter, you’ll run into a recursive call which will generate a _StackOverflowError_.
You can override Kotlin’s default getter/setter and define a custom getter and setter for the properties of your class.
Let’s take a look at this example –
https://pl.kotl.in/koiBb4rAj?theme=darcula
Constructors
In Kotlin, a class can have a primary constructor and one or more secondary constructors. In Java, a constructor initializes the member variables, however, in Kotlin the primary constructor initializes the class.
Primary Constructor
The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
https://pl.kotl.in/49OgM0XoS?theme=darcula
NOTE: The _constructor_ keyword can be omitted if the primary constructor does not have any annotations or visibility modifiers.
Then we will have something like this:
https://pl.kotl.in/kELk8i8z8?theme=darcula
In Kotlin, the primary constructor has a constrained syntax, and cannot contain any code. In order to put the initialization code (not only code to initialize properties), but initializer block is also required. It is prefixed with `init` keyword.
Let’s take a look at an example:
https://pl.kotl.in/TkWBXRFpq?theme=darcula
When you run this program, you will have this output:
ID = 1
Full Name = MICHAEL X
Cast Name = BLACK PANTHER
In the program above, parameters _id, _fullName and _castName inside the parenthesis of the class take values `1`, `Michael X`, and `Black Panther` respectively when cast object is created. However, _id, _fullName, and _castName are used without using var or val, and are not properties of the `MovieCast` class.
Looking at the `MovieCast` class, you will realize that it has 3 properties _id, _fullName and _castName declared. When `cast` object is created, the code inside the initializer block is executed. The initializer block not only initializes its properties but also prints them.
We can perform the same task in another way with this method:
https://pl.kotl.in/tTzsIF1-l?theme=darcula
We just removed some redundant lines from our code, right? I thought it would be necessary to show you a more excellent approach. Here you have it!!
Secondary Constructor
The class can also declare secondary constructors, which are prefixed with _constructor_. This is required whenever you want to create multiple constructors in Kotlin or whenever you want to include more logic in the primary constructor and you cannot do that because the primary constructor may be called by some other class.
https://pl.kotl.in/YLDZyKq00z?theme=darcula
Here is another example:
https://pl.kotl.in/G-LoQHifq?theme=darcula
In this example, the `MovieCast` class has two secondary constructors, but no primary constructor.
This class can be extended as:
https://pl.kotl.in/y4PeQn3Jn?theme=darcula
Here, constructors of the derived class `ExtendMovieCast` calls the corresponding constructor of the base class `MovieCast`. For that, `super()` is used. In addition, you can also call a constructor from another constructor of the same class (like in Java) using `this()`
https://pl.kotl.in/FBzmLGs6N?theme=darcula
In the next article, you will learn how to use Inheritance, Data classes, Interfaces and more.
No Comment! Be the first one.