This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val methodInKotlin = MethodInKotlin() | |
methodInKotlin.methodWithoutReturnTypeAndNoParameter() | |
val first = methodInKotlin.methodWithReturnTypeAndNoParameter() | |
println("return type of methodWithReturnTypeAndNoParameter: $first") | |
methodInKotlin.methodWithoutReturnTypeWithParameter("Erselan Khan") | |
val second = methodInKotlin.methodWithReturnTypeWithParameter("Erselan Khan") | |
println("return type of methodWithReturnTypeWithParameter: $second") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
// Type Checking | |
// Initializing list with String, Int and Boolean value | |
val list = listOf<Any>("Erselan Khan", 21, false) | |
// loop on above list | |
list.forEach { | |
when(it) { | |
is String -> println("String value is: $it") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
// Smart Casting | |
var studentWithRealObject: Any? = Student() | |
if (studentWithRealObject is Student) { | |
println("Student name: ${studentWithRealObject.name}") | |
println("Student age: ${studentWithRealObject.age}") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
// Non changeable values in Kotlin | |
val personAge = 27 | |
/*personAge = 26*/ // we can not reassign value to val variable | |
// Changeable values in Kotlin | |
var name = "Erselan Khan" | |
name = "Khan Erselan" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
/* | |
object classes example | |
*/ | |
val singleton = SingletonClass.age | |
println(singleton) | |
SingletonClass.updateAge(20) | |
println(SingletonClass.age) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Using open keyword to inherit this class | |
*/ | |
open class InheritableClass { | |
} | |
/* | |
Try to Inherit inheritable class | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we don't need to add open keyword | |
abstract class AbstractClasses() { | |
abstract fun someAbstractMethod() | |
} | |
class SomeClassName: AbstractClasses() { | |
override fun someAbstractMethod() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
// Multi Condition in Kotlin using when/else | |
val list = listOf("Erselan Khan", 27, "arsalankhan994@gmail.com") | |
when(list[(list.indices).random()]) { | |
"Erselan Khan" -> println("name is ${list[0]}") | |
27 -> println("age is ${list[1]}") | |
"arsalankhan994@gmail.com" -> println("email is ${list[2]}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
/* | |
Data class example | |
*/ | |
val dataClass = StudentDataClass() | |
dataClass.copy() | |
dataClass.toString() | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
/* | |
Class bound methods and variable | |
*/ | |
val someVariable = ClassBoundMethod | |
someVariable.classBoundMethod() | |
someVariable.name | |
} |