Basic of Kotlin to get started in android Part-4

Sharing is caring!

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.

interface KotlinInterfaceTest {
    val giveMyName
        get() = Sunil Gupta!"
	set(value) { }
}

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

interface KotlinInterface {

    fun addItem()
    fun deleteItem()
    fun showItems(): List<String>
    fun itemCount(){
      println("item count")
    }
}

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?

class KotlinInterfaceTest5 : KotlinInterface{

    //Any list modified inside it'll need to use a MutableList list
    var myList1: MutableList<String> = mutableListOf<String>()

    override fun addItem() {
        myList1.add("Kotlin1")
        myList1.add("Kotlin2")
        myList1.add("Kotlin3")
    }

    override fun deleteItem() {
        myList1.remove("Kotlin2")
    }

    override fun showItems(): List<String> {
        return myList1
    }

}

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?

interface KotlinInterface2 {

    fun loadItems(): List<String>
}

Let’s implement both interfaces in the same class.

class KotlinInterfaceTest5 : KotlinInterface, KotlinInterface2{

    var myList2: MutableList<String>? = null

    override fun addItem() {
        myList2 = ArrayList<String>()
        myList2!!.add("Kotlin1")  // double bang operator means it will null safely. It means it will add item if the myList2 will not null other wise throw NPE.
        myList2!!.add("Kotlin2")  
        myList2!!.add("Kotlin3")
    }

    override fun deleteItem() {
        myList2!!.remove("Kotlin2")
    }

    override fun showItems(): List<String> {
        return  myList2!!.toList()
    }

    override fun loadItems(): List<String> {
       return myList2!!.toList()
    }
}

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.

interface KotlinInterface2 {

     fun showItems(){
         print("hello KotlinInterface2");
     }
}

interface KotlinInterface {

    fun showItems(){
        print("hello KotlinInterface");
    }
}

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.

class KotlinInterfaceTest5 : KotlinInterface, KotlinInterface2{

    var myList2: MutableList<String>? = null

    override fun addItem() {
        myList2 = ArrayList<String>()
        myList2!!.add("Kotlin1")  // double bang operator means it will null safely. It means it will add item if the myList2 will not null other wise throw NPE.
        myList2!!.add("Kotlin2")  
        myList2!!.add("Kotlin3")
    }

    override fun deleteItem() {
        myList2!!.remove("Kotlin2")
    }

    override fun showItems() {
           super<KotlinInterface>.showItems()
	   super<KotlinInterface2>.showItems()
    }

}

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 🙂

0 0 votes
Article Rating
Basic of Kotlin to get started in android Part-4
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Scroll to top
0
Would love your thoughts, please comment.x
()
x