Kotlin Classes
In Kotlin, classes are a blueprint for creating objects. They can contain properties (data) and functions (behavior).
Syntax
The syntax for a class definition consists of the class keyword, followed by the name of the class, and a block of code enclosing the properties and functions of the class:
class ClassName {
// properties
// functions
}
Here are some rules for defining classes in Kotlin:
- The name of a class should start with a capital letter and should be a noun (e.g. Dog, Person, BankAccount).
- A class can have properties (data) and functions (behavior).
- Properties and functions of a class can be made public (accessible from anywhere) or private (accessible only within the class). By default, they are public in Kotlin.
- A class can have a primary constructor and/or one or more secondary constructors. The primary constructor is defined in the class header, and the secondary constructors are defined using the
constructorkeyword. - A class can have an
open,abstract,sealed, orfinalmodifier. Theopenmodifier allows the class to be subclassed, theabstractmodifier allows the class to have abstract (unimplemented) functions, thesealedmodifier allows the class to have subclasses, but only in the same file, and thefinalmodifier prevents the class from being subclassed. - A class can have an
inner,nested, orstaticnested modifier. Theinnermodifier allows the class to access the members of the outer class, thenestedmodifier allows the class to be astaticnested class, and thestaticnested modifier allows the class to be a static nested class and access only static members of the outer class. - A class can have a companion object, which is a singleton object associated with the class.
- A class can extend one or more superclasses and implement one or more interfaces.
- A class can override and/or overload functions and properties of its superclass and/or interfaces.
Examples
The following example creates a Dog class with the properties name, breed, and age and a function bark():
class Dog {var name: String = ""var breed: String = ""var age: Int = 0fun bark() {println("Woof!")}}
To create an object of this class, the constructor can be used. The properties and functions of the object can be accessed using the dot notation:
fun main(){// Constructorval myDog = Dog()// Accessing properties and functionsmyDog.name = "Fido"myDog.breed = "Labrador"myDog.age = 3myDog.bark() // Output: Woof!}
A primary constructor can also be defined in the class header:
class Dog(var name: String, var breed: String, var age: Int) {fun bark() {println("Woof!")}}fun main(){val myDog = Dog("Fido", "Labrador", 3)}
This eliminates the need to define default values for the properties and allows the creation of objects of the class using the concise constructor syntax shown above.
Secondary constructors can also be defined in a class by using the constructor keyword:
class Dog {var name: Stringvar breed: Stringvar age: Intconstructor(name: String, breed: String, age: Int) {this.name = namethis.breed = breedthis.age = age}constructor(name: String, breed: String) {this.name = namethis.breed = breedthis.age = 0}fun bark() {println("Woof!")}}fun main(){val myDog = Dog("Fido", "Labrador", 3)val myPuppy = Dog("Buddy", "Golden Retriever")}
This allows the creation of objects of the class using different sets of arguments.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours