Kotlin’s Comprehensive Class Options

Kotlin offers a variety of classes for different purposes: regular classes bundle state and operations, local classes are good for helper classes, nested classes can be private helper classes, data classes are great for storing immutable data, value classes wrap a simple type with a strong domain model, enum classes limit values pertaining to a type, sealed classes limit subtypes of a class, and annotation classes create custom annotations. Use the appropriate class for your needs to optimize performance and improve type safety. Check out a cheat sheet of all the different features of these classes in next week’s email newsletter.

In this video, we will provide an overview of all the different kinds of classes offered by Kotlin, along with general advice on when to use each one. We will cover regular classes, local classes, nested classes, data classes, value classes, enum classes, sealed classes, and annotation classes.

Key Takeaways Table

Class TypeDescription
Regular ClassBundles together state and operations in the form of properties and functions
Local ClassA class declared inside a function, used as a helper class relevant only to that function’s implementation
Nested ClassA class declared inside another class, uniquely related to the outer class
Data ClassGreat for storing data, especially immutable data, automatically implements equals and hashcode, and toString methods
Value ClassWraps a simple type with a strong domain model, improves type safety without additional object allocation
Enum ClassLimits the values that pertain to a type, can be exhaustively matched in a when expression
Sealed ClassManages different conditions that all include their own data, constrains the direct subtypes to a set of known types
Annotation ClassUsed to create custom annotations, helpful for building frameworks or tools

Regular Classes πŸ’»

A regular class bundles together state and operations in the form of properties and functions. This is your default choice for putting together state with operations that act on them. In Kotlin, we can declare a class in many different places, such as immediately inside a file, inside a function, or inside another class. When we declare a class inside a function, it is known as a local class, which is a good idea when it’s a helper class that’s only relevant to the implementation of that function. Nested classes, on the other hand, are declared inside another class and can be a good choice when it’s uniquely related to the outer class or can work great as a helper class that is unique to the implementation of that class.

Data Classes πŸ“Š

Data classes are great for storing data, especially immutable data. When we use the data modifier on a class, it automatically gets an implementation of equals and hashcode that will compare objects by value instead of by reference. Kotlin will also implement a toString method for us. Data classes can also be destructured, and since we most often use data classes with immutable objects, it includes a function called copy that creates a copy of the object with the changes that you want applied.

Value Classes πŸ”’

Value classes are used to wrap a simple type with a strong domain model, which improves type safety without additional object allocation. A value class has only one property, which has to be read-only. Value classes are especially powerful when used with JVM primitive types, as the primitive types go on the stack instead of on the heap.

Enum Classes 🎭

An enum class limits the values that pertain to a type. All of the instances of an enum class are contained within the body of the enum class, and each instance is called an entry of the enum class. Enum classes are great for referencing entries and iterating over them. They can also be exhaustively matched in a when expression, ensuring that all entries are accounted for.

Sealed Classes πŸ“¦

A sealed class manages different conditions that all include their own data. With a sealed class, we can limit the subtypes of a class to a set of known types. All subclasses are nested inside the sealed class and can be exhaustively checked in a when expression.

Annotation Classes πŸ“

Annotation classes are used to create custom annotations, which can be helpful for building frameworks or tools. When declaring an annotation class, we can use meta-annotations to indicate various things about the annotation class, such as what kind of elements it applies to and whether it can appear more than once on the same element.

Conclusion

About the Author

About the Channel:

Share the Post:
en_GBEN_GB