Polymorphism is a fundamental concept in object-oriented programming that allows objects to be treated as instances of their parent class. In Kotlin, and many other programming languages, it helps in executing a method or property that can be overridden at compile-time and runtime. Let's dive deep into understanding polymorphism in Kotlin with examples.
Compile-Time Polymorphism
Compile-time polymorphism is achieved using method overloading in Kotlin. This means you can have multiple methods with the same name but different signatures (different parameters).
fun add(a: Int, b: Int): Int {
return a + b
}
fun add(a: Double, b: Double): Double {
return a + b
}
fun add(a: String, b: String): String {
return a + b
}
In the above code, we have three overloaded add functions where Kotlin determines which method to execute during the compile-time based on the parameters passed.
Runtime Polymorphism
Runtime polymorphism in Kotlin is achieved using method overriding. This happens when a subclass has a specific implementation of a method that is already defined in its superclass.
open class Animal {
open fun sound() {
println("Animal is making a sound")
}
}
class Dog : Animal() {
override fun sound() {
println("Dog is barking")
}
}
fun main(args: Array<String>) {
val myAnimal: Animal = Dog()
myAnimal.sound() // Output: Dog is barking
}
Here, the function sound() is overridden in the Dog class. When sound() is called on an object of class Animal that is actually an instance of Dog, the subclass’s method is executed due to runtime polymorphism.
Polymorphic Methods and Properties
Kotlin allows overriding properties too, preserving the property type safety:
open class Shape {
open val area: Double
get() = 0.0
}
class Circle(val radius: Double) : Shape() {
override val area: Double
get() = Math.PI * radius * radius
}
fun main() {
val myCircle: Shape = Circle(2.5)
println("Area of the circle: ${myCircle.area}")
}
In the Circle class, the area property is overridden from the Shape class, and the overridden property calculates the area of a circle.
Final Thoughts
Understanding polymorphism is crucial for effective software architecture and design. It empowers developers to create flexible and maintainable code. Kotlin’s concise syntax and language features make using polymorphism straightforward and intuitive.