A class is a blueprint for objects that have similar properties.
In Kotlin, a class is declared using the class keyword, followed by the class name, an optional constructor, and the class body enclosed within curly brackets. All the properties and the member functions of the class are defined inside the class body.
class className {// body}
class className constructor(){// body}
If we don't specify the constructor, the compiler will generate an empty constructor on its own.
Let's see an example of a Kotlin class in the code snippet below:
// class declarationclass example {var name: String = "Shubham"var age: Int = 22fun getNameAndAge() {// body}}
example
.getNameAndAge()
.Properties in Kotlin must be eitherinitialized
or declared asabstract
.