nadirbasalamah
Posted on October 14, 2021
In object oriented programming, there is a principle called polymorphism. Polymorphism is a mechanism when a behavior can be implemented differently by many classes. For example, the human and cat entity has a walk behavior but implemented differently. The human walks with two legs whether the cat walks with four legs.
Create a Sealed Class
Sealed class is a class that contains many sub classes that inherit sealed class. Basically, sealed class is identical with enum but sealed class could contains many classes. Sealed class is an abstract class so the object cannot be created or instantiated from sealed class.
This is the basic syntax to create sealed class.
sealed class class_name(attributes,...) {
// create many sub classes..
}
The sealed class can be created with this other syntax.
sealed class class_name
// create many sub classes..
In this example, the sealed class is created inside Vehicle.kt
file.
// create a sealed class called Vehicle
sealed class Vehicle
// create two data classes that inherit Vehicle class
data class Motorcycle(val manufacturer: String, val type: String): Vehicle()
data class Car(val manufacturer: String, val type: String): Vehicle()
// create a run() method that can be called
// from a class that inherits Vehicle class
fun Vehicle.run() {
// define the run() implementation
// based on specific class
when(this) {
is Motorcycle -> println("Running with two wheels")
is Car -> println("Running with four wheels")
}
}
Create an object from Motorcycle
class that inherits sealed class called Vehicle
.
fun main() {
// create an object from Motorcycle class
val motorcycle = Motorcycle("Honda","CBR")
// call run() method
motorcycle.run()
}
Output
Running with two wheels
Based on the code above, the sealed class is created called Vehicle
that contains many sub classes including Motorcycle
and Car
.
The implementation from run()
method inside Vehicle
class is defined based on specific class with when
condition selection.
// create a run() method that can be called
// from a class that inherits Vehicle class
fun Vehicle.run() {
// define the run() implementation
// based on specific class
when(this) {
is Motorcycle -> println("Running with two wheels")
is Car -> println("Running with four wheels")
}
}
Create an Interface
Interface is a class that only contains many abstract methods. Interface can be used to define contract that has to be fulfilled for a class. This is the basic syntax to create an interface.
interface interface_name {
// create many abstract methods...
}
In this example, the interface called ShapeInterface
is created in ShapeInterface.kt
file.
interface ShapeInterface {
fun getArea(): Double
}
The interface that already created is implemented by Circle
class.
// create a Circle class that implements ShapeInterface
class Circle(val radius: Double): ShapeInterface {
// the getArea() method is implemented
override fun getArea(): Double {
return Math.PI * radius * radius
}
}
The object from Circle
class is created in main()
method.
fun main() {
// create an object from Circle class
val circle = Circle(7.0)
// call getArea() method
val area = circle.getArea()
// print out the result from getArea() method
println("Area of circle: $area")
}
Output
Area of circle: 153.93804002589985
Based on the code above, the interface called ShapeInterface
is created that contains abstract method called getArea()
. The Circle
class is implementing ShapeInterface
so all of the methods inside this interface must be implemented. In main()
method, the object from Circle
class is created then the getArea()
method is called.
The class can implements many interfaces. In this example, the Circle
class implements two interfaces.
The new interface is created called EntityInterface
in EntityInterface.kt
file.
interface EntityInterface {
fun getInfo()
}
The two interfaces is implemented in Circle
class.
// create a Circle class that implements two interfaces
class Circle(val radius: Double): ShapeInterface, EntityInterface {
// getArea() method is implemented
override fun getArea(): Double {
return Math.PI * radius * radius
}
// getInfo() method is implemented
override fun getInfo() {
println("Radius: $radius")
println("Area of circle: ${getArea()}")
}
}
The object from Circle
class is created in main()
method.
fun main() {
// create an object from Circle class
val circle = Circle(7.0)
// getInfo() method is called
circle.getInfo()
}
Output
Radius: 7.0
Area of circle: 153.93804002589985
Notes
Sealed class can be used to make many classes that inherit sealed class.
Interface can be used to define certain contract that has to be fulfilled by certain class.
This is the final part of Kotlin basic tutorial series in this blog. I hope this Kotlin basic tutorial series is helpful for learning the Kotlin programming language 😀.
Sources
Learn more about sealed class in this link.
Learn more about interface in this link.
Learn more about the example of sealed class usage in this link.
I hope this article is helpful for learning the Kotlin programming language. If you have any thoughts or comments you can write in the discussion section below.
Posted on October 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.