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.

Java

1

2

3

4

5

interfaceKotlinInterfaceTest{

val giveMyName

get()=Sunil Gupta!"

set(value){}

}

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

Java

1

2

3

4

5

6

7

8

9

interfaceKotlinInterface{

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?

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

classKotlinInterfaceTest5:KotlinInterface{

//Any list modified inside it'll need to use a MutableList list

varmyList1: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>{

returnmyList1

}

}

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?

Java

1

2

3

4

interfaceKotlinInterface2{

fun loadItems():List<String>

}

Let’s implement both interfaces in the same class.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

classKotlinInterfaceTest5:KotlinInterface,KotlinInterface2{

varmyList2: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>{

returnmyList2!!.toList()

}

override fun loadItems():List<String>{

returnmyList2!!.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.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

interfaceKotlinInterface2{

fun showItems(){

print("hello KotlinInterface2");

}

}

interfaceKotlinInterface{

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.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

classKotlinInterfaceTest5:KotlinInterface,KotlinInterface2{

varmyList2: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.

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

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com