Panduan untuk Antara muka Kotlin

1. Gambaran keseluruhan

Dalam tutorial ini, kita akan membincangkan cara menentukan dan melaksanakan antara muka di Kotlin.

Kami juga akan melihat bagaimana pelbagai antara muka dapat dilaksanakan oleh kelas. Ini pasti boleh menyebabkan konflik, dan kita akan mempelajari mekanisme yang harus dilakukan oleh Kotlin untuk menyelesaikannya.

2. Antara muka di Kotlin

Antaramuka adalah cara untuk memberikan penerangan atau kontrak untuk kelas dalam pengaturcaraan berorientasikan objek. Mereka mungkin mengandungi sifat dan fungsi secara abstrak atau konkrit bergantung pada bahasa pengaturcaraan. Kami akan melihat perincian antara muka di Kotlin.

Antaramuka di Kotlin mirip dengan antara muka dalam banyak bahasa lain seperti Java. Tetapi mereka mempunyai sintaks yang spesifik, mari kita tinjau dalam beberapa sub-bahagian berikutnya.

2.1. Menentukan Antara Muka

Mari mulakan dengan menentukan antara muka pertama kami di Kotlin:

interface SimpleInterface

Ini adalah antara muka termudah yang kosong sepenuhnya. Ini juga dikenali sebagai antara muka penanda .

Mari sekarang tambahkan beberapa fungsi ke antara muka kami:

interface SimpleInterface { fun firstMethod(): String fun secondMethod(): String { return("Hello, World!") } }

Kami telah menambahkan dua kaedah ke antara muka yang telah kami tentukan sebelumnya:

  • Salah satunya disebut f irstMethod adalah kaedah abstrak
  • Sementara yang lain disebut s econdMethod mempunyai pelaksanaan lalai.

Mari maju dan tambahkan beberapa sifat ke antara muka kami sekarang:

interface SimpleInterface { val firstProp: String val secondProp: String get() = "Second Property" fun firstMethod(): String fun secondMethod(): String { return("Hello, from: " + secondProp) } }

Di sini kami telah menambah dua sifat ke antara muka kami:

  • Salah satunya dipanggil firstProp adalah jenis String dan abstrak
  • Yang kedua disebut secondProp juga jenis rentetan tetapi ia menentukan pelaksanaan untuk aksesorinya.

Perhatikan bahawa sifat dalam antara muka tidak dapat mengekalkan keadaan . Jadi berikut adalah ungkapan tidak sah di Kotlin:

interface SimpleInterface { val firstProp: String = "First Property" // Illegal declaration }

2.2. Melaksanakan Antaramuka

Sekarang kita telah menentukan antara muka asas, mari kita lihat bagaimana kita dapat menerapkannya di kelas di Kotlin:

class SimpleClass: SimpleInterface { override val firstProp: String = "First Property" override fun firstMethod(): String { return("Hello, from: " + firstProp) } }

Perhatikan bahawa ketika kita mendefinisikan SimpleClass sebagai implementasi SimpleInterface , kita hanya perlu menyediakan implementasi untuk sifat dan fungsi abstrak . Namun, kita juga boleh mengganti harta atau fungsi yang telah ditentukan sebelumnya.

Sekarang mari kita ganti semua sifat dan fungsi yang ditentukan sebelumnya di kelas kami:

class SimpleClass: SimpleInterface { override val firstProp: String = "First Property" override val secondProp: String get() = "Second Property, Overridden!" override fun firstMethod(): String { return("Hello, from: " + firstProp) } override fun secondMethod(): String { return("Hello, from: " + secondProp + firstProp) } }

Di sini, kami telah mengatasi sifat secondProp dan fungsi secondFunction yang sebelumnya telah ditentukan dalam antara muka SimpleInterface .

2.3 Melaksanakan Antaramuka Melalui Perwakilan

Delegasi adalah corak reka bentuk dalam pengaturcaraan berorientasikan objek untuk mencapai kebolehgunaan semula kod melalui komposisi dan bukannya pewarisan . Walaupun hal ini dapat dilaksanakan dalam banyak bahasa, seperti Java, Kotlin memiliki dukungan asli untuk pelaksanaan melalui pendelegasian .

Sekiranya kita bermula dengan antara muka dan kelas asas:

interface MyInterface { fun someMethod(): String } class MyClass() : MyInterface { override fun someMethod(): String { return("Hello, World!") } }

Setakat ini, tiada perkara baru. Tetapi sekarang, kita dapat menentukan kelas lain yang menerapkan MyInterface melalui delegasi:

class MyDerivedClass(myInterface: MyInterface) : MyInterface by myInterface

MyDerivedClass expects a delegate as an argument which actually implements the interface MyInterface.

Let's see how we can call a function of the interface through delegate:

val myClass = MyClass() MyDerivedClass(myClass).someMethod()

Here we have instantiated MyClass and used that as the delegate to call functions of the interface on MyDerivedClass, which actually never implemented these functions directly.

3. Multiple Inheritance

Multiple inheritance is a key concept in the object-oriented programming paradigm. This allows for a class to inherit characteristics from more than one parent object, like an interface, for example.

While this provides more flexibility in object modeling, it comes with its own set of complexities. One such is the “diamond problem”.

Java 8 has its own mechanisms for addressing the diamond problem, as does any other language that allows for multiple inheritance.

Let's see how Kotlin addresses it through interfaces.

3.1. Inheriting Multiple Interfaces

We'll begin by defining two simple interfaces:

interface FirstInterface { fun someMethod(): String fun anotherMethod(): String { return("Hello, from anotherMethod in FirstInterface") } } interface SecondInterface { fun someMethod(): String { return("Hello, from someMethod in SecondInterface") } fun anotherMethod(): String { return("Hello, from anotherMethod in SecondInterface") } }

Note that both interfaces have methods with the same contract.

Now let's define a class which inherits from both these interfaces:

class SomeClass: FirstInterface, SecondInterface { override fun someMethod(): String { return("Hello, from someMethod in SomeClass") } override fun anotherMethod(): String { return("Hello, from anotherMethod in SomeClass") } }

As we can see, SomeClass implements both FirstInterface and SecondInterface. While syntactically this is quite simple, there is a bit of semantics that requires attention here. We will go over this in the next sub-section.

3.2. Resolving Conflicts

When implementing multiple interfaces, a class may inherit a function which has a default implementation for the same contract in multiple interfaces. This raises the problem of invocation for this function from an instance of the implementing class.

To resolve this conflict, Kotlin requires the subclass to provide an overridden implementation for such functions to make the resolution explicit.

For example, SomeClass above implements anotherMethod. But, if it didn't, Kotlin wouldn't know whether to invoke First or SecondInterface's default implementation of anotherMethod. SomeClass must implement anotherMethod for this reason.

However, someMethod is a bit different since there is actually no conflict. FirstInterface doesn't provide a default implementation for someMethod. That said, SomeClass still must implement it because Kotlin forces us to implement all inherited functions, no matter if they are defined once or multiple times in parent interfaces.

3.3. Resolving the Diamond Problem

A “diamond problem” occurs when two child objects of a base object describe a particular behavior defined by the base object. Now an object inheriting from both these child objects has to resolve which inherited behavior it subscribes to.

Kotlin's solution to this problem is through the rules defined for multiple inheritance in the previous sub-section. Let's define a few interfaces and an implementing class to present the diamond problem:

interface BaseInterface { fun someMethod(): String } interface FirstChildInterface: BaseInterface { override fun someMethod(): String { return("Hello, from someMethod in FirstChildInterface") } } interface SecondChildInterface: BaseInterface { override fun someMethod(): String { return("Hello, from someMethod in SecondChildInterface") } } class ChildClass: FirstChildInterface, SecondChildInterface { override fun someMethod(): String { return super.someMethod() } }

Here we have defined BaseInterface which declared an abstract function called someMethod. Both the interfaces FirstChildInterface and SecondChildInterface inherits from BaseInterface and implement the function someMethod.

Now as we implement ChildClass inheriting from FirstChildInterface and SecondChildInterface, it's necessary for us to override the function someMethod. However, even though we must override the method, we can still simply call super as we do here with SecondChildInterface.

4. Interfaces Compared to Abstract Classes in Kotlin

Abstract classes in Kotlin are classes which cannot be instantiated. This may contain one or more properties and functions. These properties and functions can be abstract or concrete. Any class inheriting from an abstract class must implement all inherited abstract properties and functions unless that class itself is also declared as abstract.

4.1. Differences Between Interface and Abstract Class

Wait! Doesn't that sound exactly like what an interface does?

Actually, at the outset, an abstract class is not very different from the interface. But, there are subtle differences which govern the choice we make:

  • A class in Kotlin can implement as many interfaces as they like but it can only extend from one abstract class
  • Properties in the interface cannot maintain state, while they can in an abstract class

4.2. When Should We Use What?

An interface is just a blueprint for defining classes, they can optionally have some default implementations as well. On the other hand, an abstract class is an incomplete implementation which is completed by the extending classes.

Typically interfaces should be used to define the contract, which elicits the capabilities it promises to deliver. An implementing class holds the responsibility of delivering those promises. An abstract class, however, should be used to share partial characteristics with extending classes. An extending class can take it further to complete it.

5. Comparison With Java Interfaces

With the changes to Java interface in Java 8, they have come very close to Kotlin interfaces. One of our previous articles captures the new features introduced in Java 8 including changes to the interface.

There are mostly syntactic differences between Java and Kotlin interfaces now. One difference which stands out is related to the keyword “override”. In Kotlin, while implementing abstract properties or functions inherited from an interface, it is mandatory to qualify them with the keyword “override“. There is no such explicit requirement in Java.

6. Conclusion

In this tutorial, we discussed Kotlin interfaces, how to define and implement them. Then we talked about inheriting from multiple interfaces and the conflict they may create. We took a look at how Kotlin handles such conflicts.

Akhirnya, kami membincangkan antara muka berbanding kelas abstrak di Kotlin. Kami juga secara ringkas membincangkan bagaimana perbandingan antara muka Kotlin dengan antara muka Java.

Seperti biasa, kod untuk contoh boleh didapati di GitHub.