Kotlin's outstanding features.

Alok Verma
MindOrks
Published in
3 min readNov 14, 2019

--

Kotlin is a general-purpose open-source programming language for JVM that combines object-oriented principals and functional programming features. Kotlin is 100% interoperable with Java. if you want to learn more about how Kotlin code gets converted in byte code, please go through this link-

1- Difference between object class and Singleton traditional pattern in Kotlin.

Object class works as a singleton in Kotlin, but Object class does not have constructors so we can not pass any dependency to Object class, but in case of singleton we can pass dependency as well. If there is no dependency on your singleton, then we should consider making our singleton by Object classes.

2- What are sealed classes and how does it differ from normal abstract class/Interface?

A sealed class is an abstract class and used for defining predefined hierarchy objects. Sealed classes allow us to create instances with different types, unlike Enums which restrict us to use the same type for all enum constants. A sealed class is mostly used with when keywords as we don’t have to write default/else statement for this.

example of creating sealed classes-

sealed class Animal {

class dog : Animal()

class cat : Animal()

class rat : Animal()

fun print(a:Animal){

when(a){
is dog-> print("is a dog")
is cat -> print("is a cat")
is rat -> print("is a rat")

}
}
}

3- What are extension functions in Kotlin?

Extension functions are nothing but static function in java, all they act as a class function itself. An example of extension function is — loading an image to imageView. This loadImage function is an extension function and you can call this function on any image view instance and imageView instance would be passed in this function as ‘this’ arguments.

fun ImageView.loadImage(url: String) {
Glide.with(this.context).load(url).into(this)
}

If we interpret this extension function to Java class,

public static void loadImage(String url, ImageView image) {
Glide.with(image.getContext()).load(url).into(image);
}
}

4- What are infix functions?

All infix functions are extension function but all extension functions are not infix functions. we make infix function using ‘infix’ keyword and for infix function one parameter is a must.

infix fun  Int.isXGreaterThanY(x:Int):Boolean{
return x>this
}

So, the question is why we should create an infix function if we already have extension functions.

For calling this infix function we have to use-

5 isXGreaterThanY 6

As you can see we don’t have to use a dot for calling infix function which provides better user readability for this code.

5- What are Higher Order functions in Kotlin?

As I have already created a blog post on this topic, please go through that post, link-

6- What are the data classes?

data classes are normal classes containing fields and methods for accessing them. apart from defining data classes, we can also destructure them like-

val book = Book("Roberto Bolano", "2666", 2004)

val (author, title, year) = book

Generally, we use data classes for defining our POJO.

7- What are Coroutines and how does it differ from RxJava?

Kotlin's coroutines are basically a way to achieve an interthread communication task. Just like Rxjava, we have schedulers in Rxjava to tell on which thread work will be done, the same way in coroutines have context on which work will be done. we can use either coroutines or Rxjava for achieving multithreading in android application but in Rxjava there is a lot of chance of wrong uses of operators also coroutines are very simple to write and there is less boilerplate code compare the RxJava.

I will try to add more questions to this blog in the future.

Thanks,

Alok V.

--

--

Alok Verma
MindOrks

Sr. Software Engineer @OYO, Cinema fanatic, Technology enthusiast.