Kotlin - Class & Object Tutorial


In this article, we will learn basics about class and its object in Kotlin programming language.

What is class and object?
A class is a blueprint of a runtime entity and object is its state, which includes both its behavior and state.

In Kotlin, class declaration consists of a class header and a class body, similar to Java.Class can be declared using the keyword class.

Syntax:
class className { //class head
//Class body
}

Example:
class myClass {
   // property (data member)
   private var name: String = "Kotlin"
   
   // Member function
   fun printMe() {
      print("You Are Learning -"+name)
   }
}
fun main(args: Array<String>) {
   val obj = myClass() // Creating an object of myClass class
   obj.printMe() // Calling member function of myClass 
}
Output: You Are Learning -Kotlin

Comments