Classes and Objects (part-I)
Explore Kotlin's approach to classes and objects, including how constructors and properties work. Learn to write compact and readable code by understanding primary constructors, property declarations, and default visibility settings. This lesson helps you grasp Kotlin's object-oriented basics with practical clarity.
We'll cover the following...
Overview
Classes in Kotlin offer many improvements over those in Java, resulting in more compact and better code.
Kotlin, like Java, is an object-oriented programming language with a remarkable number of functional programming features.
Kotlin offers classes with encapsulation, inheritance, polymorphism, and all other object-oriented paradigms, also known from Java.
On the other hand, the syntax is a bit different, enabling you to write more compact, easier-to-read, and on the whole, simply better code.
A simple class example
As in Java, the declaration of a class starts with the keyword, class, followed by its name, Person.
In our example class, Person has two fields. More precisely, these are properties of the String type named firstName and lastName.
The
Stringtype is automatically inferred by the compiler due to the initializations withStringliterals. You might also have declared the type manually, for example,val firstName : String = "Alex".
Public is the default
The first difference to Java is the visibility of those fields. While in Java, those would be package-private, since nothing else is specified, the default in Kotlin is public, so that any properties and functions not declared otherwise are always publicly visible.
However, this class is extremely inflexible and therefore meaningless. The two properties have fixed values that are assigned to them when creating an instance. Since they are immutable, they cannot be reassigned at all.
Therefore, the class needs a constructor:
Constructors
Unlike Java, Kotlin makes a distinction between the primary constructor and any secondary ones.
The primary constructor of a class can be written very concisely by simply appending its arguments directly to the class name:
The parameters noted here, fn and ln, can be accessed in the class body so that the name of a person can now be specified individually when creating an instance.
Declaring properties directly in the constructor
In the common case of directly assigning constructor arguments to properties, Kotlin offers a very compact and useful alternative syntax:
class Person(val firstName: String, val lastName: String)
This does not only declare a primary constructor with two parameters. In fact, these two parameters are also declared directly to properties of the class, simply by the prefix val or var. Therefore, this example is identical to the previous one.
The class has become so short and compact that even the entire class body including the {} can be omitted. However, it still remains a correctly declared Kotlin class. It might look somewhat more accustomed with a few line breaks added:
It might look somewhat more accustomed with a few line breaks added: