Search⌘ K

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.

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

Kotlin 1.5
class Person {
val firstName = "Alex"
val lastName = "Prufrock"
}

As in Java, the declaration of a class starts with the keyword, ...