Classes

The class keyword defines class. Unlike Java, the name of the class can be different from the name of the file in which the class resides. Additionally, file can contains several top-level classes.

The . operator is used to access to the members from outside of class.

By default class and its members have public scope.

// empty class
class ClassName

class ClassName2{
    val title = "Unknown" 
    var chapters: Int = 0
    var author: String? = null
    
    fun printChapterCount(){
        println("${title} has ${chapters}")
    }
}

val clsName2 = ClassName2() // create instance of class
println(clsName2.title) // print title property in console