Kotlin classes, data classes, sealed classes, objects, companion objects, interfaces, delegation and extension functions.
39 cards · basic cards · AI-written, checked twice. Edit anything.
- What is the primary purpose of a data class in Kotlin?
Show answer
To hold data with automatically generated equals(), hashCode(), toString(), copy() and componentN() functions
- How do you declare a data class in Kotlin?
Show answer
data class ClassName(val prop1: Type1, val prop2: Type2)
- What does the copy() function do on a data class?
Show answer
Creates a shallow copy of the object, allowing you to modify specific properties in the new instance
- What is a sealed class in Kotlin?
Show answer
A class that restricts which subclasses can inherit from it; all subclasses must be defined in the same file or nested
- Why use a sealed class instead of an abstract class?
Show answer
Sealed classes provide exhaustive when expression checking, ensuring all subclass types are handled
- How do you declare a singleton object in Kotlin?
Show answer
object SingletonName { ... }
- What is the difference between object and class in Kotlin?
Show answer
object declares a singleton instance; class declares a template for creating multiple instances
- What is a companion object in Kotlin?
Show answer
An object declared inside a class that can access private members and holds static-like properties and methods
- How do you call a companion object member?
Show answer
ClassName.memberName (no instance needed, like static members in Java)
- Can a class have more than one companion object?
Show answer
No, a class can have at most one companion object
- How do you declare an interface in Kotlin?
Show answer
interface InterfaceName { fun method(): ReturnType; val property: Type }
- Can Kotlin interfaces have properties?
Show answer
Yes, interfaces can have abstract properties and properties with default implementations
- What is delegation in Kotlin?
Show answer
A design pattern where an object forwards method calls to another object, using the by keyword
- How do you implement delegation in Kotlin?
Show answer
class Derived(val base: Base) : Base by base { ... }
- What is an extension function in Kotlin?
Show answer
A function added to an existing class without inheritance or modification, using receiver.method() syntax