<p>This article is about interfaces and how to implement it in Kotlin with the help of simple examples.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Interfaces</span></strong></h3>



<p>In Kotlin, Interfaces can contain declarations of abstract methods (have no body), as well as method implementation of non-abstract methods. ;</p>



<p>Interfaces can have properties but these need to be abstract or to provide accessor implementations.</p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Creating Interface</span></strong></h5>



<p>To declare an interface, we use <strong><span style="color:#0000ff" class="has-inline-color">interface</span></strong> keyword.</p>



<pre class="wp-block-preformatted"><strong><span style="color:#04603e" class="has-inline-color">//By default interfaces and </span></strong>
//<strong><span style="color:#04603e" class="has-inline-color"><strong>abstract members(properties and methods)</strong> </span></strong>
//<strong><span style="color:#04603e" class="has-inline-color">are public abstract</span></strong>
<strong><span style="color:#0000ff" class="has-inline-color">public abstract</span></strong> <strong>interface </strong>MusicPlayer {
 <strong><span style="color:#0000ff" class="has-inline-color">public abstract</span></strong> <strong>var defaultVolume</strong>: String <em><strong><span style="color:#04603e" class="has-inline-color">// abstract property</span></strong>

 </em><strong><span style="color:#0000ff" class="has-inline-color">public abstract</span> fun </strong>play() <span style="color:#04603e" class="has-inline-color"><strong> <em>// abstract function</em></strong></span><em>
 </em><strong>fun </strong>stop() = <strong>"the MusicPlayer is in stop mode" </strong><em><strong><span style="color:#04603e" class="has-inline-color">// function with default implementation</span></strong>
</em>}</pre>



<p>Here, <strong>MusicPlayer</strong> is an interface which contains a property <strong>defaultVolume</strong> , abstract function <strong>play()</strong> and non-abstract function <strong>stop()</strong> .</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">Note:</span></strong> Interfaces and Interface members (abstract and non-abstract) are always open. You do not need to explicitly use <strong><span style="color:#0000ff" class="has-inline-color">open</span></strong> keyword.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Implementing an Interface</span></strong></h3>



<p class="p2">Interfaces cannot be instantiated because it contains abstract members (have no implementation). But, we can implement interfaces in other classes.</p>



<p>The class which implements Interface must <strong><span style="color: #008000;">override</span></strong> all abstract members (abstract properties and abstract methods) of Interface or you have to declare class as <span style="color: #008000;"><strong>abstract</strong></span> class.</p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong> </h5>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//Implementing an interface example</span></strong>
</em><strong>interface </strong>MusicPlayer {

 <strong>fun </strong>play() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract function</span></strong>

 </em><strong>fun </strong>stop() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract function</span></strong>

 <strong><span style="color:#04603e" class="has-inline-color">//non-abstract function with default implementation</span></strong>
 </em><strong>fun </strong>setVolume(x: Int) {

 System.<em>out</em>.println(<strong>"Player volume Changed by: " </strong>+ x);
 }

}

<em><strong><span style="color:#04603e" class="has-inline-color">//MP3Player class implementing MusicPlayer interface.
//It must override all the abstract functions of MusicPlayer interface.</span></strong>
</em><strong>class </strong>MP3Player : MusicPlayer {

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of play abstract function</span></strong>
 </em><strong>override fun </strong>play() {
 <em>println</em>(<strong>"the MP3 Player is playing."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of stop abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>stop() {
 <em>println</em>(<strong>"the MP3 Player is off."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of setVolume <em><strong>function</strong></em> </span></strong>
 </em><strong>override fun </strong>setVolume(x: Int) {

 <strong>if </strong>(x <;= 100) {
 System.<em>out</em>.println(<strong>"Player volume Changed by: " </strong>+ x);
 } <strong>else </strong>{
 System.<em>out</em>.println(<strong>"Player volume level should be less than 100."</strong>);
 }
 }
}

<strong>fun </strong>main() {
 <strong>val </strong>mP3Player = MP3Player()
 mP3Player.play()
 <em><strong><span style="color:#04603e" class="has-inline-color">//setVolume <em><strong>function</strong></em> of MP3Player class get executed</span></strong>
 </em>mP3Player.setVolume(110)
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">the MP3 Player is playing.
Player volume level should be less than 100.</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example explained</span></strong></h5>



<ul class="wp-block-list" id="block-69491672-b201-486f-ace5-5fddb5ee3629"><li><strong>MusicPlayer</strong> is an interface which contains a non-abstract function setVolume() and two abstract functions play() and stop().</li><li>The class <strong>MP3Player</strong> implements interface <strong>MusicPlayer</strong> and overrides its functions (abstract and non-abstract functions). </li><li><strong>mP3Player</strong> is an object of MP3Player class. The statement <strong>mP3Player.play()</strong> will execute the play function and <strong>mP3Player.setVolume()</strong> will execute the setVolume function of MP3Player class.</li></ul>



<p><strong><span style="color:#0000ff" class="has-inline-color">Note:</span></strong> If a class implementing the Interface does not override a non-abstract function of Interface, then the compiler will execute the default implementation of that function within the Interface.</p>



<h3 class="wp-block-heading"><strong><span style="color:#530599" class="has-inline-color">Properties in Interfaces</span></strong></h3>



<p>You can declare properties in interfaces. A property declared in an interface can either be <strong><span style="color:#04603e" class="has-inline-color">abstract</span></strong>, or it can <strong><span style="color:#04603e" class="has-inline-color">provide implementations for accessors</span></strong>. </p>



<p>Properties declared in interfaces can&#8217;t have <strong><span style="color:#04603e" class="has-inline-color">backing fields</span></strong>, and therefore accessors declared in interfaces can&#8217;t reference them.</p>



<h5 class="wp-block-heading"><strong><strong><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong></strong></strong> </h5>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//interface</span></strong>
</em><strong>interface </strong>Addition {

 <strong>val numOne</strong>: Int <em><strong><span style="color:#04603e" class="has-inline-color">// abstract property</span></strong>

 </em><strong>val numTwo</strong>: Int <em><strong><span style="color:#04603e" class="has-inline-color">//property with implementation</span></strong>
 </em><strong>get</strong>() = 6

 <em><strong><span style="color:#04603e" class="has-inline-color">//function with default implementation</span></strong>
 </em><strong>fun </strong>add() {
 <em>println</em>(<strong>"Sum of $numOne and $numTwo is: " </strong>+ (<strong>numOne </strong>+ <strong>numTwo</strong>))
 }

}

<strong>class </strong>Number : Addition {

 <strong>override val numOne </strong>= 5

 override <strong>fun </strong>add() {
 <em><strong><span style="color:#04603e" class="has-inline-color">//add <em><strong>function</strong></em> of Addition Interface get executed</span></strong>
 </em><strong>super</strong>.add()
 }
}

<strong>fun </strong>main() {
 <strong>val </strong>number = Number()
 <em><strong><span style="color:#04603e" class="has-inline-color">//add <em><strong>function</strong></em> of Number class get executed</span></strong>
 </em>number.add()
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">Sum of 5 and 6 is: 11</pre>



<h3 class="wp-block-heading"><strong><span style="color:#530599" class="has-inline-color">Interface reference type</span></strong></h3>



<p>We cannot create an object of any interface but Interfaces can be used as <strong>reference type</strong> for the object of an implementing class. </p>



<p>Using interface reference type we can only access its own functions and properties.</p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong> </h5>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//Interface</span></strong> 
</em><strong>interface </strong>MusicPlayer {

 <strong>fun </strong>play() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

 </em><strong>fun </strong>stop() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

</em>}

<em><strong><span style="color:#04603e" class="has-inline-color">//MP3Player class implementing MusicPlayer interface.</span></strong>
</em><strong>class </strong>MP3Player : MusicPlayer {

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of play abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>play() {
 <em>println</em>(<strong>"the MP3 Player is playing."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of stop abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>stop() {
 <em>println</em>(<strong>"the MP3 Player is in stop mode."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//MP3Player class non-abstract <em><strong>function</strong></em></span></strong>
 </em><strong>fun </strong>pause() {
 <em>println</em>(<strong>"the MP3 Player is in pause mode."</strong>)
 }
}

<strong>fun </strong>main() {
 <strong><span style="color:#04603e" class="has-inline-color">//Using interface reference type</span></strong> 
 <strong>val </strong>mP3Player: <strong>MusicPlayer</strong> = MP3Player()
 mP3Player.play()
 mP3Player.stop()
 <em><span style="color:#04603e" class="has-inline-color"><strong>//using interface reference type we can only access its own members.
 </strong></span><strong><span class="has-inline-color has-black-color">//mP3Player.pause()</span></strong><span style="color:#04603e" class="has-inline-color"><strong> //error: interface MusicPlayer does not contain any pause <em><strong>function</strong></em></strong></span>
</em>}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">the MP3 Player is playing.<br>the MP3 Player is in stop mode.</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Multiple inheritance by interface</span></strong></h3>



<p class="p1">Kotlin does not support multiple inheritance which means that a class cannot extend more than one class.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<p class="p1">But, multiple inheritance using interfaces is possible. We can implement multiple Interfaces in a single class.</p>



<p class="p2">A class implementing Interfaces must implement all the abstract methods of all the Interfaces.</p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong> </h5>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//Interface</span></strong>
</em><strong>interface </strong>MusicPlayer {

 <strong>fun </strong>play() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

 </em><strong>fun </strong>stop() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>
</em>}

<em><strong><span style="color:#04603e" class="has-inline-color">//Interface</span></strong></em>
<strong>interface </strong>VideoPlayer {

 <strong>fun </strong>changeBrightness(x: Int) <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

 </em><strong>fun </strong>changeContrast(x: Int) <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>
</em>}

<em><strong><span style="color:#04603e" class="has-inline-color">//SmartPhone class implementing interfaces MusicPlayer,VideoPlayer</span></strong>
</em><strong>class </strong>SmartPhone : MusicPlayer, VideoPlayer {

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of play abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>play() {
 <em>println</em>(<strong>"Player is playing."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of stop abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>stop() {
 <em>println</em>(<strong>"Player stopped."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of changeBrightness abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>changeBrightness(x: Int) {
 <em>println</em>(<strong>"Brightness Changed by: " </strong>+ x)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of changeContrast abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>changeContrast(x: Int) {
 <em>println</em>(<strong>"Contrast Changed by: " </strong>+ x)
 }
}

<strong>fun </strong>main() {
 <strong>val </strong>smartPhone = SmartPhone()
 smartPhone.play()
 smartPhone.changeBrightness(30)
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">Player is playing.<br>Brightness Changed by: 30</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Resolving overriding conflicts (multiple Interfaces having same method)</span></strong></h3>



<p>In case both the implemented interfaces contain non-abstract method (let&#8217;s say<strong> ;<code>next()</code> ;</strong>method) with same method signature, the implementing class must override the next() method and this method must explicitly specify which next() method is to be used.</p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong> </h5>



<pre class="wp-block-preformatted"><strong><span style="color:#04603e" class="has-inline-color">//Interface</span></strong>
<strong>interface </strong>MusicPlayer {

 <span style="color:#04603e" class="has-inline-color"> <strong>//fun play()

 //fun stop()</strong></span>
<em>
<strong><span style="color:#04603e" class="has-inline-color">//next <em><strong><em><strong>function</strong></em></strong></em> of MusicPlayer Interface</span></strong></em>
//<em><strong><span style="color:#04603e" class="has-inline-color">with default implementation</span></strong>
 </em><strong>fun </strong>next() {
 <em>println</em>(<strong>"Next from MusicPlayer"</strong>)
 }
}

<strong><span style="color:#04603e" class="has-inline-color">//Interface</span></strong>
<strong>interface </strong>VideoPlayer {

 <strong><span style="color:#04603e" class="has-inline-color">//fun changeBrightness(x: Int)

 //fun changeContrast(x: Int)</span></strong>
<em><strong>
</strong><em><strong><span style="color:#04603e" class="has-inline-color">//next <em><strong><em><strong>function</strong></em></strong></em> of VideoPlayer Interface</span></strong></em></em>
//<em><em><strong><span style="color:#04603e" class="has-inline-color">with default implementation</span></strong></em>
 </em><strong>fun </strong>next() {
 <em>println</em>(<strong>"Next from VideoPlayer"</strong>)
 }
}

<em><strong><span style="color:#04603e" class="has-inline-color">//SmartPhone class implementing interfaces MusicPlayer,VideoPlayer</span></strong>
</em><strong>class </strong>SmartPhone : VideoPlayer, MusicPlayer {

 <em><strong><span style="color:#04603e" class="has-inline-color">//overriding next() <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>next() {

 <em><strong><span style="color:#04603e" class="has-inline-color">//use super keyword to call the next
 //<em><strong>function</strong></em> of MusicPlayer interface</span></strong>
 </em><strong>super</strong><;MusicPlayer>;.next()

 <em><strong><span style="color:#04603e" class="has-inline-color">//use super keyword to call the next
 //<em><strong>function</strong></em> of VideoPlayer interface</span></strong>
 </em><strong>super</strong><;VideoPlayer>;.next()
 }
}

<strong>fun </strong>main() {
 <strong>val </strong>smartPhone = SmartPhone()
 <strong><span style="color:#04603e" class="has-inline-color"> <em>//next <em><strong>function</strong></em> of SmartPhone class get executed</em></span></strong><em>
 </em>smartPhone.next()
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">Next from MusicPlayer<br>Next from VideoPlayer</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Inheriting an Interface</span></strong></h3>



<p>An interface can inherit ;another interface or interfaces (more than one interface) .</p>



<p>Any class that implements an interface must implement the abstract members declared in that interface plus all the <strong><span style="color:#04603e" class="has-inline-color">abstract members</span></strong> that are present in the super interface.</p>



<p>If the implementing class is <strong><span style="color:#04603e" class="has-inline-color">abstract</span></strong> it may choose to implement all, some or none of the methods declared in the interface. </p>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Example</span></strong> </h5>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//Interfaces inheritance example</span></strong>
</em><strong>interface </strong>Player {

 <strong>fun </strong>play() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

 </em><strong>fun </strong>stop() <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>
</em>}

<em><strong><span style="color:#04603e" class="has-inline-color">//VideoPlayer interface inheriting Player interface</span></strong>
</em><strong>interface </strong>VideoPlayer : Player {

 <strong>fun </strong>changeBrightness(x: Int) <em><strong><span style="color:#04603e" class="has-inline-color">//abstract <em><strong>function</strong></em></span></strong>

 <strong><span style="color:#04603e" class="has-inline-color">//function with default implementation</span></strong>
 </em><strong>fun </strong>next() {
 System.<em>out</em>.println(<strong>"Next from VideoPlayer"</strong>)
 }
}

<em><strong><span style="color:#04603e" class="has-inline-color">//SmartPhone class implementing VideoPlayer interface</span></strong>
</em><strong>class </strong>SmartPhone : VideoPlayer {

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of play abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>play() {
 <em>println</em>(<strong>"video playing.."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of stop abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>stop() {
 <em>println</em>(<strong>"video stopped.."</strong>)
 }

 <em><strong><span style="color:#04603e" class="has-inline-color">//implementation of changeBrightness abstract <em><strong>function</strong></em></span></strong>
 </em><strong>override fun </strong>changeBrightness(x: Int) {
 <em>println</em>(<strong>"Brightness Changed by: " </strong>+ x)
 }
}

<strong>fun </strong>main() {
 <strong>val </strong>smartPhone = SmartPhone()
 smartPhone.play()
 smartPhone.changeBrightness(30)
 smartPhone.next()
}</pre>



<h5 class="wp-block-heading"><strong><span style="color:#0000ff" class="has-inline-color">Output</span></strong></h5>



<pre class="wp-block-preformatted">video playing..<br>Brightness Changed by: 30<br>Next from VideoPlayer</pre>



<h4 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Points to Remember:</span></strong></h4>



<ul class="wp-block-list" id="block-c1b41eaa-6411-4180-bade-8bbefbce6d18"><li>Interfaces can contain declarations of <span style="color:#04603e" class="has-inline-color"><strong>abstract methods</strong> </span>(have no body), as well as method implementations. ;</li><li>Interface also contains properties which can either be abstract, or it can provide implementations for accessors.</li><li>Interfaces cannot be instantiated. They can only be implemented by an implementing class or extended by another interface.</li><li>Interfaces can be used as <strong><span style="color:#04603e" class="has-inline-color">reference type</span></strong> for the object of an implementing class.</li><li>A class that implements interface must provide implementation to all the abstract methods of interface or you have to declare it as <strong><span style="color:#04603e" class="has-inline-color">abstract</span></strong> class.</li><li>A class can implement more than one interface.</li><li>An interface can <strong>inherit</strong> more than one interface.</li><li>Kotlin does not support multiple inheritance using class, but by using <strong>interface</strong> it can achieve multiple inheritance.</li><li>Interface is used to achieve abstraction and loose coupling.</li></ul>


