In Kotlin, you can restrict enums by using the sealed
keyword before the enum
keyword when declaring your enum class. This makes the enum class sealed, meaning that all subclasses must be declared within the same file as the sealed class itself. This restricts the ability to extend the enum class outside of the file in which it is declared, ensuring that all possible values are known and accounted for within the enum class definition.
What is an enum class in Kotlin?
An enum class in Kotlin is a type that represents a group of constants (also known as enumeration constants). Enum classes provide a way to define a fixed set of constant values that can be used as a data type in your code. Each constant value in an enum class is an instance of the enum class itself.
Here is an example of an enum class in Kotlin:
1 2 3 4 5 6 |
enum class Direction { NORTH, SOUTH, EAST, WEST } |
In this example, Direction
is an enum class that contains four constant values: NORTH
, SOUTH
, EAST
, and WEST
. Enum classes in Kotlin can also have properties and methods just like regular classes.
You can use enum classes to represent a set of related constants in your code and provide type safety when working with those constants.
How to declare a constructor in Kotlin?
To declare a constructor in Kotlin, you can use the constructor
keyword followed by parentheses containing any parameters for the constructor. Here is an example:
1 2 3 4 5 6 7 |
class Person(firstName: String, lastName: String) { // Primary constructor constructor(firstName: String, lastName: String, age: Int) : this(firstName, lastName) { // Secondary constructor println("Person created with following details: $firstName $lastName $age") } } |
In this example, we have a primary constructor that takes firstName
and lastName
parameters, and a secondary constructor that also takes an age
parameter. The secondary constructor delegates to the primary constructor using the this
keyword.
How to use companion objects in Kotlin?
In Kotlin, a companion object is a special kind of object that is tied to a specific class, rather than creating instances of that class. You can define a companion object inside a class by using the companion object
keyword.
Here is an example of how to use companion objects in Kotlin:
1 2 3 4 5 6 7 8 9 10 11 |
class MyClass { companion object { fun printMessage() { println("Hello from companion object") } } } fun main() { MyClass.printMessage() // Call the function in companion object } |
In this example, the MyClass
class has a companion object with a function printMessage()
. To call the function from the companion object, you don't need to create an instance of MyClass
, you can directly call MyClass.printMessage()
.
Companion objects can also have properties and can implement interfaces. They are often used to define static methods or constants related to the class.
Keep in mind that companion objects are created when the class is loaded, making them useful for storing global constants or initializing resources.
How to prevent instantiation of enum classes in Kotlin?
To prevent instantiation of enum classes in Kotlin, you can use the private constructor and declare the enum class as a sealed class. By making the constructor private, you ensure that the enum class cannot be instantiated outside of its own class.
Here is an example of how you can prevent instantiation of an enum class in Kotlin:
1 2 3 4 5 6 7 8 |
sealed class MyEnum { object A : MyEnum() object B : MyEnum() companion object { fun values() = arrayOf(A, B) } } |
In this example, the MyEnum
class is declared as a sealed class and has private constructors for the enum objects A and B. This prevents any other class from instantiating objects of the MyEnum
class. Additionally, the values()
function is used to provide a list of the enum values.