This file contains hidden or 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() { | |
/* | |
Without Extension Functions | |
if we are using a class "SomeThirdPartyClass" and | |
we want to add one more function, then | |
we need to inherit the "SomeThirdPartyClass" class with | |
"MyClass" and write that function inside it and | |
use "MyClass" everywhere instead of "SomeThirdPartyClass" class | |
*/ |
This file contains hidden or 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() { | |
/* | |
Without Extension Functions | |
if we are using a class "SomeThirdPartyClass" and | |
we want to add one more function, then | |
we need to inherit the "SomeThirdPartyClass" class with | |
"MyClass" and write that function inside it and | |
use "MyClass" everywhere instead of "SomeThirdPartyClass" class | |
*/ |
This file contains hidden or 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() { | |
/* | |
Primary Constructor Example | |
*/ | |
val primaryConstructorOnly = PrimaryConstructorOnly() | |
println("My name is ${primaryConstructorOnly.name}") | |
println("My age is ${primaryConstructorOnly.age}") | |
val primaryConstructorWithParams = PrimaryConstructorWithParams( |
This file contains hidden or 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 | |
} |
This file contains hidden or 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 hidden or 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, "[email protected]") | |
when(list[(list.indices).random()]) { | |
"Erselan Khan" -> println("name is ${list[0]}") | |
27 -> println("age is ${list[1]}") | |
"[email protected]" -> println("email is ${list[2]}") |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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" |
NewerOlder