<p class="p1">This article is about interfaces and how to implement it in Java with the help of examples.</p>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><b>Interface</b></span></h3>



<p class="p2">An interface in java is a collection of fields and abstract methods <span style="color: #000000;">(only method signature, no body)</span>.</p>



<ul class="wp-block-list"><li>Interfaces are by default prefix with <span style="color: #008000;"><strong>abstract </strong><span style="color: #000000;">keyword</span><strong>, </strong></span>which means similar to abstract classes we cannot create an object of interface.</li><li class="p2">All methods in an interface are by default <span style="color: #008000;"><strong>public</strong></span> <strong><span style="color: #008000;">abstract </span></strong><span style="color: #000000;">(only method signature, no body).</span></li><li class="p2">All properties (data fields) in an interface are <strong><span style="color: #008000;">public</span></strong> <span style="color: #008000;"><strong>static final </strong><span style="color: #000000;">and </span></span>must be initialized at the time of declaration.</li></ul>



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



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



<pre class="wp-block-preformatted"><strong><span style="color: #0000ff;"><span style="color: #008000;">//by default interface members are </span>public
</span></strong><span style="color: #000000;">abstract <strong><span style="color: #0000ff;">interface</span></strong></span> MusicPlayer{
<span style="color: #008000;"><strong> //declare constant fields are</strong> <strong>public static final</strong></span>
 <span style="color: #000000;">public static final </span>int defaultVolume = 30;
 <span style="color: #008000;"><strong>//declare methods are</strong> <strong>public abstract(no body)</strong></span>
 <span style="color: #000000;">public abstract </span>void play();
 <span style="color: #000000;">public abstract </span>void stop();
}</pre>



<p>Here, <span style="color: #0000ff;"><strong>MusicPlayer</strong></span> is an interface which contains a field <span style="color: #008000;"><strong>defaultVolume</strong></span> and two abstract methods( play() and stop() ).</p>



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



<p class="p2">Interfaces cannot be instantiated because it contains abstract methods (have no body). 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 methods of Interface or you have to declare it as <span style="color: #008000;"><strong>abstract</strong></span> class.</p>



<p>In Java, we use the <strong><span style="color: #0000ff;">implements</span></strong> ;keyword to implement interfaces.</p>



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



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//Implementing an interface example.</strong></span>
<strong><span style="color: #0000ff;">interface</span></strong> MusicPlayer {
<span style="color: #008000;"><strong> //play() and stop() are abstract methods</strong></span>
 public <span style="color: #0000ff;"><strong>abstract</strong></span> void play();

 public <strong><span style="color: #0000ff;">abstract</span></strong> void stop();

}
<strong><span style="color: #008000;">//MP3Player class implementing MusicPlayer interface.
//It must override all the abstract methods of MusicPlayer interface.</span></strong>
class MP3Player <span style="color: #0000ff;"><strong>implements</strong></span> MusicPlayer {

<span style="color: #008000;"><strong> //implementation of play abstract method </strong></span>
 public void play() {
 System.out.println("the MP3 Player is playing.");
 }
<strong><span style="color: #008000;"> //implementation of stop abstract method</span> </strong>
 public void stop() {
 System.out.println("the MP3 Player is off.");
 }
}

class Main {
 public static void main(String[] args) {
 MP3Player mp3Player = new MP3Player();
 mp3Player.play();
 }
}</pre>



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



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



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



<ul class="wp-block-list"><li class="p1"><span style="color: #0000ff;"><strong>MusicPlayer</strong></span> is an interface which contains two abstract methods play() and stop().</li><li class="p1">To create an object of <span style="color: #008000;"><strong>MP3Player</strong></span>, we must provide an implementation for the play() and stop() methods inside MP3Player class.</li><li class="p1">Now, by creating an object of MP3Player we can call the methods of Interface (play() and stop()).</li></ul>



<h3 class="wp-block-heading"> ;</h3>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Interface reference variable</strong></span></h3>



<p>We cannot create an object of any interface but we can create reference variable of interface that refers to the object of its implementing class.</p>



<p>Using interface reference variable we can only access its own methods and variables.</p>



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



<pre class="wp-block-preformatted"><strong><span style="color: #008000;">//Interface reference variable example.</span></strong>
interface MusicPlayer {
<span style="color: #008000;"><strong> //play() and stop() are abstract methods</strong></span>
 void play();

 void stop();
}
<strong><span style="color: #008000;">//MP3Player class implementing MusicPlayer interface</span></strong>
class MP3Player implements MusicPlayer {
<span style="color: #008000;"><strong> //MP3Player class method</strong></span>
 public void pause() {
 System.out.println("the MP3 Player is in pause state");
 }
<strong><span style="color: #008000;"> //implementation of play abstract method</span> </strong>
 public void play() {
 System.out.println("the MP3 Player is playing.");
 }
<strong><span style="color: #008000;"> //implementation of stop abstract method</span> </strong>
 public void stop() {
 System.out.println("the MP3 Player is off.");
 }
}

class Main {
 public static void main(String[] args) {
 MusicPlayer musicPlayer = new MP3Player();

 musicPlayer.play(); <strong><span style="color: #008000;">//the MP3 Player is playing.

 //error: MusicPlayer does not contain pause() method. </span></strong>
 <strong> //musicPlayer.pause();</strong>
 }
}</pre>



<h3 class="wp-block-heading"> ;</h3>



<h3 class="h2 wp-block-heading"><span style="color: #000080;"><strong>Multiple inheritance by interface</strong></span></h3>



<p class="p1">Java does not support multiple inheritance which means that a class cannot extend more than one class.</p>



<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"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//Multiple inheritance by interface example.</strong></span>
interface MusicPlayer {
<span style="color: #008000;"><strong> //play() and stop() are abstract methods</strong></span>
 void play();

 void stop();
}

interface VideoPlayer {
<span style="color: #008000;"><strong> //changeContrast() and changeBrightness() are abstract methods</strong></span>
 void changeContrast(int x);

 void changeBrightness(int x);
}
<strong><span style="color: #008000;">//Smartphone class implementing interfaces</span> <span style="color: #008000;">MusicPlayer,VideoPlayer</span></strong>
class Smartphone implements MusicPlayer, VideoPlayer {
<strong><span style="color: #008000;"> //implementation of play abstract method</span> </strong>
 public void play() {
 System.out.println("playing...");
 }
<strong><span style="color: #008000;"> //implementation of stop abstract method</span> </strong>
 public void stop() {
 System.out.println("stopped...");
 }

<strong><span style="color: #008000;"> //implementation of changeContrast abstract method</span> </strong>
 public void changeContrast(int x) {
 System.out.println("Constrast Changed by: " + x);
 }

<strong><span style="color: #008000;"> //implementation of changeBrightness abstract method</span> </strong>
 public void changeBrightness(int x) {
 System.out.println("Brightness Changed by: " + x);
 }
}

class Main {
 public static void main(String[] args) {
 Smartphone smartphone = new Smartphone();
 smartphone.play();
 smartphone.changeBrightness(20);
 }
}</pre>



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



<pre class="wp-block-preformatted">playing...
Brightness Changed by: 20</pre>



<h3 class="wp-block-heading" id="inheriting-an-interface"> ;</h3>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Interface Default methods</strong></span></h3>



<p class="p1">Before Java 8, interface contains only abstract methods. The implementation of these methods has to be provided in a class which implements the interface.</p>



<p class="p1">So, if a new method is to be added in an interface, then its implementation code has to be provided in all the classes implementing the same interface.</p>



<p class="p1">To overcome this issue, Java 8 now provides us <span style="color: #0000ff;"><strong>default methods</strong> </span>for Interfaces.</p>



<p class="p1"><strong><span style="color: #008000;">Default methods</span></strong> allow the interfaces to have methods with implementation without affecting the classes that implement the interface.</p>



<p>If a class does not implement a default method, the compiler will take the implementation mentioned within the Interface.</p>



<p class="p1">To declare default methods inside interfaces, we use the ;<strong><span style="color: #0000ff;">default</span></strong> ;keyword.</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>




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



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//Interface default methods example</strong></span>
interface MusicPlayer {
<strong><span style="color: #008000;"> //play() and stop() are abstract methods</span></strong>
 void play();

 void stop();
<strong><span style="color: #008000;"> //default method</span></strong>
 default void setVolume(int x) {

 System.out.println("Player volume Changed by: " + x);
 }
}

<strong><span style="color: #008000;">//MP3Player class implementing MusicPlayer interface</span></strong>
class MP3Player implements MusicPlayer {
<strong><span style="color: #008000;"> //implementation of play abstract method</span> </strong>
 public void play() {
 System.out.println("the MP3 Player is playing.");
 }
<strong><span style="color: #008000;"> //implementation of stop abstract method</span> </strong>
 public void stop() {
 System.out.println("the MP3 Player is off.");
 }
<strong><span style="color: #008000;"> //implementation of setVolume default method</span> </strong>
 public void setVolume(int x) {
 if (x <;= 100) {
 System.out.println("Player volume Changed by: " + x);
 } else {
 System.out.println("Player volume level should be less then 100.");
 }
 }
}

class Main {
 public static void main(String[] args) {
 MP3Player mp3Player = new MP3Player();
 mp3Player.play();
<strong><span style="color: #008000;"> //setVolume default method defined in MP3Player class get executed</span></strong>
 mp3Player.setVolume(110);

 }
}</pre>



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



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



<h3 class="wp-block-heading"> ;</h3>



<h3 class="p1 wp-block-heading"><span style="color: #000080;"><b>Default Methods and Multiple Inheritance</b></span></h3>



<p class="p1">In case both the implemented interfaces contain default methods with same method signature, the implementing class must override the default method and must explicitly specify which default method is to be used.</p>



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



<pre class="wp-block-preformatted"><span style="color: #008000;"><strong>//multiple inheritance through default methods example.</strong></span>
interface MusicPlayer {

<strong><span style="color: #008000;"> //default method</span></strong>
 default void next() {
 System.out.println("Next from MusicPlayer");
 }
}

interface VideoPlayer {

 <strong><span style="color: #008000;">//default method</span></strong>
 default void next() {
 System.out.println("Next from VideoPlayer");
 }
}
<strong><span style="color: #008000;">//Smartphone class implementing MusicPlayer and VideoPlayer interface</span></strong>
class Smartphone implements MusicPlayer, VideoPlayer {

<strong><span style="color: #008000;">//overriding default next() method</span></strong>
 public void next() {
 <strong><span style="color: #008000;">//use super keyword to call the next </span></strong>
<strong><span style="color: #008000;"> //method of MusicPlayer interface</span></strong>
 MusicPlayer.super.next();

 <strong><span style="color: #008000;">//use super keyword to call the next </span></strong>
<strong><span style="color: #008000;"> //method of VideoPlayer interface</span></strong>
 VideoPlayer.super.next();
 }
}

class Main {
 public static void main(String[] args) {
 Smartphone smartphone = new Smartphone();
<strong><span style="color: #008000;"> //default next() method executed</span></strong>
 smartphone.next();

 }
}</pre>



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



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



<h3 class="wp-block-heading"> ;</h3>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Interface static methods</strong></span></h3>



<p class="p1">Java 8 also provides the ability to add static methods to interfaces.</p>



<p class="p1">Static methods in interfaces are almost identical to static methods in normal classes. The only big difference is that static methods are not inherited in the classes that implements the interface.</p>



<p class="p1">This means that we can access static methods of an interface using its references only ;and not by the object of a class which implements the interface.</p>



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



<pre class="wp-block-preformatted"><strong><span style="color: #008000;">//Interface static method example</span></strong>
interface MusicPlayer {
<strong><span style="color: #008000;"> //static method</span></strong>
 public static void print() {
 System.out.println("MusicPlayer static method called..");
 }
<strong><span style="color: #008000;"> //abstract method</span></strong>
 void play();

}
<strong><span style="color: #008000;">//MP3Player class implementing MusicPlayer interface</span></strong>
class MP3Player implements MusicPlayer {
<strong><span style="color: #008000;"> //implementation of play abstract method</span> </strong>
 public void play() {
 System.out.println("the MP3 Player is playing.");
 }

}

class Main {
 public static void main(String[] args) {
 MP3Player mp3Player = new MP3Player();
 
 MusicPlayer.print(); <strong><span style="color: #008000;">// static method executed</span></strong>
 <strong>// mp3Player.print();</strong> <strong><span style="color: #008000;">// This will cause a compilation error
 }</span></strong>
}</pre>



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



<pre class="wp-block-preformatted">MusicPlayer static method called..</pre>



<h3 class="wp-block-heading" id="inheriting-an-interface"> ;</h3>



<h3 class="wp-block-heading" id="inheriting-an-interface"><span style="color: #000080;"><strong>Inheriting an Interface</strong></span></h3>



<p class="p1">An interface can <span style="color: #000000;">extend</span> another interface or interfaces (more than one interface) .</p>



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



<p class="p1">In Java, ; <strong><span style="color: #0000ff;">extends</span></strong> ;keyword is used for extending interfaces.</p>



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



<pre class="wp-block-preformatted"><strong><span style="color: #008000;">//Inheriting an interface example.</span></strong>
interface Player {
<strong><span style="color: #008000;"> //abstract methods play() and stop()</span></strong>
 public void play();

 public void stop();
}
<span style="color: #008000;"><strong>//VideoPlayer extending Player interface</strong></span>
interface VideoPlayer extends Player {
<strong><span style="color: #008000;"> //abstract method</span></strong>
 void changeBrightness(int x);

<strong><span style="color: #008000;"> //default method</span></strong>
 default public void next() {
 System.out.println("Next from VideoPlayer");
 }
}
<strong><span style="color: #008000;">//class must implement abstract methods of VideoPlayer and Player</span></strong>
class SmartPhone implements VideoPlayer {

<strong><span style="color: #008000;"> //implementation of play abstract method</span> </strong>
 public void play() {
 System.out.println("video playing..");
 }
<strong><span style="color: #008000;"> //implementation of stop abstract method</span> </strong>
 public void stop() {
 System.out.println("video stopped..");
 }
<strong><span style="color: #008000;"> //implementation of changeBrightness abstract method</span> </strong>
 public void changeBrightness(int x) {
 System.out.println("Brightness Changed by: " + x);
 }
}

class Main {
 public static void main(String[] args) {
 SmartPhone smartphone = new SmartPhone();
 smartphone.play();
 smartphone.changeBrightness(20);

 }
}</pre>



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



<pre class="wp-block-preformatted">video playing..
Brightness Changed by: 20</pre>



<h4 class="wp-block-heading"> ;<span style="color: #000080;"><b>Points to Remember:</b></span></h4>



<ol class="ol1 wp-block-list"><li>Interface contains <span style="color: #0000ff;"><b>methods </b></span><span style="color: #008000;"><strong>(implicitly public abstract)</strong></span> and <span style="color: #0000ff;"><b>fields </b></span><span style="color: #008000;"><strong>(implicitly public static final)</strong></span><b>.</b></li><li>In Java 8 and above, Interfaces can have <strong><span style="color: #008000;">default methods</span></strong> and <strong><span style="color: #008000;">static methods</span></strong>.</li><li>An Interface cannot be instantiated but we can create <strong><span style="color: #008000;">reference variable</span> </strong>of it that refers to the object of its implemented 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: #0000ff;">abstract</span></strong> class.</li><li>Interface do not have <strong><span style="color: #008000;">constructors</span></strong>.</li><li>To inherit an interface within a class we need to use <strong><span style="color:#0000ff" class="has-inline-color">implements</span></strong> keyword and to inherit an interface in another interface we need to use <strong><span style="color:#0000ff" class="has-inline-color">extends</span></strong> keyword.</li><li>A class can <span style="color: rgb(0, 128, 0);"><strong>implement</strong></span> more than one interface.</li><li>An interface can <strong><span style="color: #008000;">extends</span></strong> more than one interface.</li><li>Java does not support multiple inheritance using class, but by using <strong><span style="color: #008000;">interface</span></strong> it is possible.</li><li>Interface is used to achieve abstraction and loose coupling.</li></ol>


