android-kotlin
/

Kotlin Classes – Structure, Constructors & Types

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

android-kotlin

Kotlin Classes – Structure, Constructors & Types

Modern Class Declaration

In Kotlin, classes are declared using the class keyword. The most efficient feature is the Primary Constructor, defined directly in the class header. This allows you to declare and initialize properties in a single line, drastically reducing the boilerplate code found in Java.

KOTLINRead-only
1
// Standard class with primary constructor
class Project(val id: String, var title: String, val isAiGenerated: Boolean = false) {
    init {
        // Initialization logic runs here
        println("Project $title initialized.")
    }
}

// Usage: No 'new' keyword needed (just like Dart)
val myProject = Project("rev_01", "Mobile UI Builder")

  1. Data Classes: The Model Standard

When you need a class strictly to hold data—such as a JSON response for a Revochamp widget—you use a data class. The Kotlin compiler automatically generates equals(), hashCode(), toString(), and the invaluable copy() method. This is a massive upgrade over manual Dart model implementation.

KOTLINRead-only
1
data class Widget(val id: String, val type: String)

val button = Widget("b1", "Button")
// Create a copy with a modified property
val icon = button.copy(type = "Icon")

  1. Sealed Classes: State Management

Sealed classes represent restricted class hierarchies. For an Engineering Manager, they are the 'Gold Standard' for representing UI States (Loading, Success, Error). When used with a when expression, the compiler ensures you have handled every possible state, preventing runtime crashes.

KOTLINRead-only
1
sealed class GenerationState {
    object Idle : GenerationState()
    object Loading : GenerationState()
    data class Success(val code: String) : GenerationState()
    data class Failure(val error: String) : GenerationState()
}

// Pattern matching ensures all cases are covered
fun handleState(state: GenerationState) = when(state) {
    is GenerationState.Idle -> println("Waiting...")
    is GenerationState.Loading -> showSpinner()
    is GenerationState.Success -> renderCode(state.code)
    is GenerationState.Failure -> showError(state.error)
}

  1. Objects and Companion Objects

Kotlin doesn't use the static keyword. For Singletons, you use the object keyword. For members that should belong to a class (like constants or factory methods), you use a companion object. This keeps the namespace clean and organized.

KOTLINRead-only
1
class RevoEngine {
    companion object {
        const val VERSION = "2.0.0"
        fun create() = RevoEngine()
    }
}

// Usage: RevoEngine.VERSION

Class Type Comparison

Class TypePrimary PurposeKey Feature
Standard ClassGeneral logic/UIEncapsulation & Inheritance
Data ClassData ModelsAutogenerated copy/toString
Sealed ClassState/Type safetyExhaustive pattern matching
ObjectSingletonsThread-safe lazy initialization
Abstract ClassTemplatesCannot be instantiated

Test Your Knowledge

Q1
of 3

Which keyword is used to create a class specifically for holding data with autogenerated utility methods?

A
model
B
data
C
sealed
D
value
Q2
of 3

How do you allow a Kotlin class to be inherited by another class?

A
public
B
open
C
extendable
D
abstract
Q3
of 3

Which structure is used in Kotlin to replace static methods and constants within a class?

A
StaticBlock
B
Companion Object
C
GlobalObject
D
InnerClass

Frequently Asked Questions

What is the difference between a Data Class and a regular Class?

Data classes automatically provide methods for structural equality (==) and copying. Regular classes require you to override these methods manually. Data classes also require at least one property in the primary constructor.

Why are classes 'final' by default in Kotlin?

To promote 'Composition over Inheritance.' If you want a class to be subclassable, you must explicitly mark it with the 'open' keyword. This prevents accidental overriding of logic in complex native modules.

Can a Sealed Class be instantiated?

No. A Sealed Class is abstract by nature. You can only instantiate its subclasses. This makes it perfect for representing a closed set of types.

Previous

kotlin classes

Next

kotlin interfaces

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.