Guida alle interfacce di Kotlin

1. Panoramica

In questo tutorial, discuteremo come definire e implementare le interfacce in Kotlin.

Daremo anche uno sguardo a come più interfacce possono essere implementate da una classe. Ciò può certamente causare conflitti e impareremo il meccanismo che Kotlin ha per risolverli.

2. Interfacce in Kotlin

Un'interfaccia è un modo per fornire una descrizione o un contratto per le classi nella programmazione orientata agli oggetti. Possono contenere proprietà e funzioni in modi astratti o concreti a seconda del linguaggio di programmazione. Esamineremo i dettagli delle interfacce in Kotlin.

Le interfacce in Kotlin sono simili alle interfacce in molti altri linguaggi come Java. Ma hanno una sintassi specifica, esaminiamoli nelle prossime sottosezioni.

2.1. Definizione delle interfacce

Cominciamo definendo la nostra prima interfaccia in Kotlin:

interface SimpleInterface

Questa è l'interfaccia più semplice che è completamente vuota. Questi sono noti anche come interfacce marker .

Aggiungiamo ora alcune funzioni alla nostra interfaccia:

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

Abbiamo aggiunto due metodi alla nostra interfaccia definita in precedenza:

  • Uno di questi chiamato f irstMethod è un metodo astratto
  • Mentre l'altro chiamato s econdMethod ha un'implementazione predefinita.

Andiamo avanti e aggiungiamo ora alcune proprietà alla nostra interfaccia:

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

Qui abbiamo aggiunto due proprietà alla nostra interfaccia:

  • Uno di loro chiamato firstProp è di tipo String ed è astratto
  • Anche il secondo chiamato secondProp è di tipo stringa ma definisce un'implementazione per la sua funzione di accesso.

Notare che le proprietà in un'interfaccia non possono mantenere lo stato . Quindi la seguente è un'espressione illegale in Kotlin:

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

2.2. Implementazione delle interfacce

Ora che abbiamo definito un'interfaccia di base, vediamo come possiamo implementarla in una classe in Kotlin:

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

Nota che quando definiamo SimpleClass come un'implementazione di SimpleInterface , dobbiamo solo fornire l'implementazione per proprietà e funzioni astratte . Tuttavia, possiamo sovrascrivere anche qualsiasi proprietà o funzione definita in precedenza.

Sostituiamo ora tutte le proprietà e le funzioni definite in precedenza nella nostra classe:

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) } }

Qui, abbiamo sovrascritto la proprietà secondProp e la funzione secondFunction che erano state precedentemente definite nell'interfaccia SimpleInterface .

2.3 Implementazione delle interfacce tramite delega

La delega è un modello di progettazione nella programmazione orientata agli oggetti per ottenere la riusabilità del codice attraverso la composizione anziché l'ereditarietà . Sebbene sia possibile implementarlo in molti linguaggi, come Java, Kotlin ha il supporto nativo per l'implementazione tramite delega .

Se iniziamo con un'interfaccia e una classe di base:

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

Finora niente di nuovo. Ma ora possiamo definire un'altra classe che implementa MyInterface tramite delega:

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.

Infine, abbiamo discusso le interfacce rispetto alle classi astratte in Kotlin. Abbiamo anche parlato brevemente di come l'interfaccia Kotlin si confronta con l'interfaccia Java.

Come sempre, il codice per gli esempi è disponibile su GitHub.