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.
- 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.
- 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.
- 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.
Class Type Comparison
| Class Type | Primary Purpose | Key Feature |
|---|---|---|
| Standard Class | General logic/UI | Encapsulation & Inheritance |
| Data Class | Data Models | Autogenerated copy/toString |
| Sealed Class | State/Type safety | Exhaustive pattern matching |
| Object | Singletons | Thread-safe lazy initialization |
| Abstract Class | Templates | Cannot be instantiated |