Skip to content

Instantly share code, notes, and snippets.

View arsalankhan994's full-sized avatar

Erselan Khan arsalankhan994

View GitHub Profile
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
*/
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
*/
fun main() {
/*
Primary Constructor Example
*/
val primaryConstructorOnly = PrimaryConstructorOnly()
println("My name is ${primaryConstructorOnly.name}")
println("My age is ${primaryConstructorOnly.age}")
val primaryConstructorWithParams = PrimaryConstructorWithParams(
main() {
/*
Class bound methods and variable
*/
val someVariable = ClassBoundMethod
someVariable.classBoundMethod()
someVariable.name
}
main() {
/*
Data class example
*/
val dataClass = StudentDataClass()
dataClass.copy()
dataClass.toString()
}
/*
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]}")
// we don't need to add open keyword
abstract class AbstractClasses() {
abstract fun someAbstractMethod()
}
class SomeClassName: AbstractClasses() {
override fun someAbstractMethod() {
}
/*
Using open keyword to inherit this class
*/
open class InheritableClass {
}
/*
Try to Inherit inheritable class
*/
main() {
/*
object classes example
*/
val singleton = SingletonClass.age
println(singleton)
SingletonClass.updateAge(20)
println(SingletonClass.age)
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"