Restricting Class Hierarchies in Kotlin
Last Updated :
02 Mar, 2022
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. In this article, we will learn how to restrict the class hierarchies in Kotlin. Before we move ahead, you should know some basic concepts of OOPs and familiarity with class and properties of classes like inheritances.
So, When we are sure that a value or a class can have only a limited set of types or number of subclasses, that’s when we try to restrict class hierarchy. Yes, this might sound like an enum class but, actually, it’s much more than that. Enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances that can contain a state. Let’s look at an example in the mentioned steps.
Step by Step Implementation
Step 1. We will create a sealed class named ToastOperation. Under the same source file, we will define a ShowMessageToast subclass:
class ShowMessageToast(val message:String):ToastOperation()
Step 2. Also, we’ll define a ShowErrorToast object:
object ShowErrorToast:ToastOperation()
Step 3. As you may have noted, we have defined an object rather than a full class declaration, because the howErrorToast object doesn’t have any state. Also, by doing so, we have removed it from the when block, since there is just one instance. Now, we can use it in a when statement, as follows:
Kotlin
fun doToastOperation (toastOperation: ToastOperation) {
when (toastOperation) {
is ShowMessageToast
->Toast.makeText( this , toastOperation.message, Toast.LENGTH_LONG).show()
ShowErrorToast->Toast . makeText ( this , "Error...Grr!" ,Toast.LENGTH_LONG).show()
}
}
|
Step 4. The key benefit is that we don’t need to implement the else block, which acted as the default block when the other statements didn’t fit the bill. According to documentation, a sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself. However, the subclasses of subclasses need not be defined in the same file. It is abstract by itself, and you cannot instantiate objects from it. Here’s our structure of sealed classes:
sealed class ToastOperation {
}
object ShowErrorToast:ToastOperation()
class ShowMessageToast (val message:String):ToastOperation()
As you can see, we’ve kept all the subclasses under the same source file in which we have defined the sealed class. In the preceding example, we were sure that we can only have two types of toasts: an error toast and a toast with a custom message. So we created a sealed class ToastOperation and created two subclasses of ToastOperation. Note that if we aren’t sure of types of subclasses, we will not use a sealed class, in that case, an enum class might be better suited.
If you are using Kotlin versions prior to 1.1, you’ll need to implement the subclasses inside the sealed class, much like this:
sealed class ToastOperation {
object ShowErrorToast:ToastOperation()
class ShowMessageToast(val message:String):ToastOperation()
}
Note: You can use the preceding way in the new version of Kotlin as well.
Similar Reads
Function Return and Type Hierarchy in Kotlin
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, App code, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
4 min read
How to Get the Class in Kotlin?
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Kotlin Collections Ordering
There are certain collections where the order of the elements is important. If you take lists, two lists are never equal if they are ordered differently inspite of having same elements.In Kotlin, there are different ways of ordering the elements. Most built-in types are comparable: -Numeric types fo
4 min read
How to Work With Nested Class in Kotlin?
A class is declared within another class then it is called a nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating an object of the class. Syntax of declaration:Â class outerClass { ............ // outer class p
3 min read
Kotlin Nested class and Inner class
Nested Class In Kotlin, you can define a class inside another class, which is known as a nested class. Nested classes have access to the members (fields and methods) of the outer class. Here is an example of a nested class in Kotlin: C/C++ Code class Car { var make: String var model: String var year
5 min read
Delegated Properties in Kotlin
Delegation is defined as the granting of any authority or power to another person (Boss assigning tasks to its employees) to carry out different work. However, the person who delegated the work remains accountable for the outcome of the delegated work. In a similar manner, there are various types of
5 min read
Code Formatting in Kotlin using ktlint
As we all know about the kotlin language which is recommended by google particularly for android app development, and of course, it made the life of android app developers easier. But if you are a beginner in this field then you may not know that you need to write the codes in the desired format. an
4 min read
Kotlin Inline classes
Inline classes are introduced by Kotlin since Kotlin 1.3 version to overcome the shortcomings of traditional wrappers around some types.These Inline classes add the goodness of Typealiases with the value range of the primitive data types. Let us suppose that we are selling some items and the cost is
4 min read
Merge Two Collections in Kotlin
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
4 min read
Closures in Kotlin
According to Kotlin's official documentation "A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time the closure was created" var sum = 0 ints.f
2 min read