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
constructor
keyword. - A class can have an
open
,abstract
,sealed
, orfinal
modifier. Theopen
modifier allows the class to be subclassed, theabstract
modifier allows the class to have abstract (unimplemented) functions, thesealed
modifier allows the class to have subclasses, but only in the same file, and thefinal
modifier prevents the class from being subclassed. - A class can have an
inner
,nested
, orstatic
nested modifier. Theinner
modifier allows the class to access the members of the outer class, thenested
modifier allows the class to be astatic
nested class, and thestatic
nested 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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours