Generics practical in kotlin

 package com.example.my_kotlin_practice


fun main(args: Array<String>)
{
val obj = GenericsExample<String>("Android")
val obj1 = GenericsExample<Int>(10)
val obj2 = GenericsExample<Double>(22.45)
val obj3 = GenericsExample<Boolean>(true)

val intBox = Box(100)
val stringBox = Box("Kotlin")
val doubleBox = Box(90.56)
val booleanBox = Box(true)
println("Int is : ${intBox.value}")
println("String is : ${stringBox.value}")
println("Double is : ${doubleBox.value}")
println("Boolean is : ${booleanBox.value}")
}
class GenericsExample<T>(input:T) {
init {
println("I am getting called with the value : $input")
}
}
class Box<T>(val value:T)

OUTPUT :

I am getting called with the value : Android I am getting called with the value : 10 I am getting called with the value : 22.45 I am getting called with the value : true Int is : 100 String is : Kotlin Double is : 90.56 Boolean is : true Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin