Junior — Middle
Can you list the types of classes used in Kotlin?
sobes.tech AI
Answer from AI
The following class types are used in Kotlin:
- Regular classes — standard classes for describing objects.
- Abstract classes — classes that cannot be instantiated directly and may contain abstract methods.
- Interfaces — define a set of methods and properties that must be implemented by classes.
- Object declarations — singletons that create a single instance of a class.
- Companion objects — objects within a class that allow the creation of static members.
- Data classes — classes designed for storing data, automatically generating methods like equals, hashCode, toString, and copy.
- Nested and inner classes — classes declared within other classes; inner classes have a reference to the outer class.
- Sealed classes — classes with a limited set of subclasses, useful for expressing restricted hierarchies.
Example of a data class:
data class User(val name: String, val age: Int)