Assessments
Chapter 1, Getting Started with Kotlin
Question 1
What’s the difference between var
and val
in Kotlin?
Answer
The val
keyword is used to declare an immutable reference, which means the value it holds cannot be changed once it’s assigned. On the other hand, the var
keyword declares a mutable reference, allowing the value it holds to be reassigned multiple times.
Question 2
How do you extend a class in Kotlin?
Answer
To extend a class, you specify a colon followed by the superclass name and its constructor. If the superclass is a regular class, it must be declared as open
, as, by default, Kotlin classes are final
and cannot be extended unless explicitly allowed using the open
keyword.
Question 3
How do you add functionality to a final
class?
Answer
To add functionality to a final
class in Kotlin, which cannot be inherited due to its finality, one can utilize extension functions. These functions enable you to ...