> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Class

A class in Kotlin is a user-defined blueprint for creating objects, declared using the `class` keyword. It encapsulates state and behavior through properties and member functions. Structurally, a Kotlin class consists of a class name, an optional class header (containing type parameters and the primary constructor), and an optional class body enclosed in curly braces. If a class has no body, the curly braces can be omitted entirely.

```kotlin theme={"dark"}
class EmptyClass
```

## Constructors and Initialization

Kotlin distinguishes between a **primary constructor** and one or more **secondary constructors**.

**Primary Constructor**
The primary constructor is part of the class header. It is declared immediately after the class name and any type parameters. If the primary constructor does not have annotations or visibility modifiers, the `constructor` keyword can be omitted. Declaring properties (`val` or `var`) directly in the primary constructor automatically initializes them as class members.

```kotlin theme={"dark"}
class Person(val firstName: String, var age: Int)
```

The primary constructor cannot contain arbitrary execution code. Initialization logic must be placed within **initializer blocks**, denoted by the `init` keyword. During instance initialization, `init` blocks are executed in the exact order they appear in the class body, interleaved with property initializers.

```kotlin theme={"dark"}
class Person(name: String) {
    val customerKey = name.uppercase()

    init {
        require(name.isNotBlank()) { "Name cannot be blank" }
    }
}
```

**Secondary Constructors**
Secondary constructors are declared within the class body using the `constructor` keyword. If a class has a primary constructor, every secondary constructor must delegate to the primary constructor, either directly or indirectly through another secondary constructor, using the `this` keyword.

```kotlin theme={"dark"}
class Person(val name: String) {
    var children: MutableList<Person> = mutableListOf()

    // Secondary constructor delegating to the primary constructor
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}
```

## Properties and Backing Fields

Properties in Kotlin classes are declared as mutable using `var` or read-only using `val`. Kotlin automatically generates getters for all properties, and setters for `var` properties.

If a property requires a custom accessor that references the property's stored value, Kotlin provides an implicit backing field accessible via the `field` identifier. The backing field is only generated if the property uses the default implementation of at least one accessor, or if a custom accessor references it through the `field` identifier.

```kotlin theme={"dark"}
class Rectangle(val width: Int, val height: Int) {
    // Custom getter, no backing field generated
    val area: Int
        get() = width * height 

    var color: String = "White"
        set(value) {
            // 'field' refers to the generated backing field
            field = value.trim() 
        }
}
```

## Inheritance and Modifiers

By default, all classes in Kotlin are `final` at the bytecode level, meaning they cannot be subclassed. To permit inheritance, a class must be explicitly marked with the `open` modifier. All Kotlin classes implicitly inherit from the `Any` root class, which provides default implementations for `equals()`, `hashCode()`, and `toString()`.

```kotlin theme={"dark"}
open class Base(val p: Int)

class Derived(p: Int) : Base(p)
```

## Instantiation

Kotlin does not utilize the `new` keyword. To instantiate a class, the constructor is invoked using standard function invocation syntax.

```kotlin theme={"dark"}
val person = Person("Alice", 30)
val derived = Derived(42)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
