Classes and Constructors
In Kotlin, classes are declared using the class keyword. The most striking difference from Java is the Primary Constructor, which is part of the class header itself. This makes property declaration incredibly concise. As an Architect, you'll appreciate how this reduces 'boilerplate' noise in your native Android models.
- Data Classes: The Perfect Models
When a class is created primarily to hold data (like a JSON response from your Python backend), you mark it as a data class. The compiler automatically generates equals(), hashCode(), toString(), and the powerful copy() function. This is far more advanced than standard Dart classes and saves hours of manual coding.
- Inheritance and 'open' Classes
By default, all classes in Kotlin are final (cannot be inherited). To allow a class to be a parent, you must explicitly mark it with the open keyword. This 'Design for Inheritance' philosophy prevents accidental architectural fragility in your native Android modules.
- Sealed Classes: State Management Pro
Sealed classes are used for representing restricted class hierarchies. They are essentially 'Enums on steroids.' For a Flutter Architect, they are the ideal way to represent UI States (Loading, Success, Error) when passing data back through a Method Channel.
- Objects and Companion Objects
Kotlin doesn't have static members. Instead, it uses object for Singletons and companion object for members that belong to a class. This makes the Singleton pattern a first-class language feature.
| Feature | Kotlin OOP | Dart (Flutter) OOP |
|---|---|---|
| Default Visibility | Public | Public |
| Immutability | Classes are final by default | Classes are open by default |
| Static Members | Companion Objects | static keyword |
| Data Models | Data Classes (Autogen) | Manual / Code Gen |
| Enums | Enums / Sealed Classes | Enums / Sealed Classes |
| Constructors | Primary & Secondary | Generative & Named |