Horje
Polymorphism in Kotlin

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.  

Real-life Illustration: Polymorphism

A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.

Types of polymorphism

Polymorphism is mainly divided into two types:  

  1. Compile-time Polymorphism
  2. Runtime Polymorphism

Examples

Example 1: Compile-time Polymorphism

Let’s start with compile-time polymorphism. In compile-time polymorphism, the name functions, that is, the signature remains the same but parameters or return type is different. At compile time, the compiler then resolves which functions we are trying to call based on the type of parameters and more. Check out this example:

Kotlin

fun main (args: Array<String>) {
  println(doubleof(4))
  println(doubleof(4.3))
  println(doubleof(4.323))
}
 
fun doubleof(a: Int):Int {
  return 2*a
}
 
fun doubleOf(a:Float):Float {
  return 2*a
}
 
fun doubleof(a:Double):Double {
  return 2.00*a
}

Output:

8
8.6
8.646

Example 2: Runtime Polymorphism

Now, let’s talk about run-time polymorphism. In run-time polymorphism, the compiler resolves a call to overridden/overloaded methods at runtime. We can achieve run-time polymorphism using method overriding. Let’s try an example where we extend a superclass and override one of its member methods:

Kotlin

fun main(args: Array<string>){
  var a = Sup()
  a.method1()
  a.method2()
   
  var b = Sum()
  b.method1()
  b.method2()
}
 
open class Sup{
   open fun method1(){
       println("printing method 1 from inside Sup")
   }
   fun method2(){
       println("printing method 2 from inside Sup")
   }
}
 
class Sum:Sup(){
   override fun method1(){
       println("printing method 1 from inside Sum")
   }
}

Output:

printing method 1 from inside Sup
printing method 2 from inside Sup
printing method 1 from inside Sum
printing method 2 from inside Sup

Here, the compiler resolves, at run-time, which method to execute.




Reffered: https://www.geeksforgeeks.org


Kotlin

Related
Implementing Complicated Interfaces with Multiple Overridden Methods in Kotlin Implementing Complicated Interfaces with Multiple Overridden Methods in Kotlin
Closures in Kotlin Closures in Kotlin
How to Iterate Over a Class&#039;s Properties in Kotlin? How to Iterate Over a Class&#039;s Properties in Kotlin?
Named Parameters in Kotlin Named Parameters in Kotlin
How to Use try - catch as an Expression in Kotlin? How to Use try - catch as an Expression in Kotlin?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8