Basic of Kotlin to get started in android Part-4

As we have practiced basic features of Kotlin in my last tutorial series of basic of Kotlin to get started on Android. If you haven’t checked yet then I would be recommended for checking all my posts here. Basic of Kotlin to get started in Android part-1 and Basic of Kotlin to get started in Android part-2 and Basic of Kotlin to get started in Android part-3.

Ok great. In this tutorial, we will learn some of the cool feature added in kotlin.

Interface in Kotlin

An interface in kotlin is similar to Java. But one thing change in Koltin is that interfaces are not fully abstract. As we know that interface in Java is fully abstract in below Java 8. It means that all methods of an interface will be abstract. Whenever we want to use this interface in any class we need to implement by prefixed keyword implements.

But in Kotin it is not mandatory that all method required abstract. It means that it can be declaration and definition. And even it is not required to use any prefixed keyword to use in any of the class. Let’s see few examples.

Properties in Interface:

Default implementations also work for property getters and setters.

Let’s see the other example of an abstract method.

In above example, we have noticed that interface have both abstract and concrete method. It means that interface is not fully abstract. Ok, Let’s see how can we implement this interface in a class to instantiate?

Now we can instantiate the KotlinInterfaceTest5 class and access those methods to execute.  As we know that Java also supports to implements multiple interfaces. Kotlin is also supporting to implements multiple interfaces. Let see how?

Let’s implement both interfaces in the same class.

Here I used List to add items. Apparently, the default List of Kotlin is immutable. Moreover, Array in Kotlin is mutable whereas List is not. Here I used to double bang operator It means it will be null safely. It will only add an item to list if the myList2 will not be null otherwise it will through NPE (NullPointerException). For more detail about to know null safely please check the official site.

Conflicts when Implementing Multiple Interfaces with Default Implementations: It might be a chance that we can use the same abstract method in both of interface then it will be conflict. The compiler gets confused to which one can execute.  Lets modified the KotlinIterface2 interface and KotlinIterface.

So we need to take care while using both interfaces in a class to implement. We need to delegate to the default implementation of which interface.

Wrapping up:  Kotlin is a great language as we have seen. You can do more practice to use this features to better understand to work seamlessly. Ok sounds great.  You can get more details about the interface in Kotlin from kotlin official site.

In my next tutorial, we will learn some of more awesome features of Kotlin.

Please do subscribe your email to get the newsletter on this blog and if you feel good about this post then do not forget to share and comment below.

Happy Coding 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *