Files
freeCodeCamp/guide/english/kotlin/classes/index.md
Samuel Koprivnjak 1e99510ebc Control Flow for Kotlin lang (#26793)
* Create control_flow.md

* Rename control_flow.md to controlflow.md

* Added control flow

* Added classes

* fix: corrected indentation

* fix: corrected indentation
2019-05-26 17:57:17 +05:30

956 B

title
title
Classes

Classes

Basic Usage

Declaration

class keyword is used to define the class
It is consisted of: class name, class header (type parameters, constructor) and class body (curly braces)

class Person { ... }

Constructor

There are multiple ways to define class constructor.
Primary constructor is part of the class header (name field with data type):

class Person(name: String) { ... }

Secondary constructor: using constructor keyword inside class body

class Person {
  constructor(parent: Person) {
    parent.children.add(this)
  }
}

Usage

Class instance can be created as regular function
There is no need for new keyword

val john = Person("John Wayne")

Resources