This article is about interfaces and how to implement it in Java with the help of examples.
Interface
An interface in java is a collection of fields and abstract methods (only method signature, no body).
- Interfaces are by default prefix with abstract keyword, which means similar to abstract classes we cannot create an object of interface.
- All methods in an interface are by default public abstract (only method signature, no body).
- All properties (data fields) in an interface are public static final and must be initialized at the time of declaration.
Creating Interface
To declare an interface, we use interface keyword.
//by default interface members are public abstract interface MusicPlayer{ //declare constant fields are public static final public static final int defaultVolume = 30; //declare methods are public abstract(no body) public abstract void play(); public abstract void stop(); }
Here, MusicPlayer is an interface which contains a field defaultVolume and two abstract methods( play() and stop() ).
Implementing an Interface
Interfaces cannot be instantiated because it contains abstract methods (have no body). But, we can implement interfaces in other classes.
The class which implements Interface must override all abstract methods of Interface or you have to declare it as abstract class.
In Java, we use the implements keyword to implement interfaces.
Example
//Implementing an interface example. interface MusicPlayer { //play() and stop() are abstract methods public abstract void play(); public abstract void stop(); } //MP3Player class implementing MusicPlayer interface. //It must override all the abstract methods of MusicPlayer interface. class MP3Player implements MusicPlayer { //implementation of play abstract method public void play() { System.out.println("the MP3 Player is playing."); } //implementation of stop abstract method 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(); } }
Output
the MP3 Player is playing.
Example explained
- MusicPlayer is an interface which contains two abstract methods play() and stop().
- To create an object of MP3Player, we must provide an implementation for the play() and stop() methods inside MP3Player class.
- Now, by creating an object of MP3Player we can call the methods of Interface (play() and stop()).
Interface reference variable
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.
Using interface reference variable we can only access its own methods and variables.
Example
//Interface reference variable example. interface MusicPlayer { //play() and stop() are abstract methods void play(); void stop(); } //MP3Player class implementing MusicPlayer interface class MP3Player implements MusicPlayer { //MP3Player class method public void pause() { System.out.println("the MP3 Player is in pause state"); } //implementation of play abstract method public void play() { System.out.println("the MP3 Player is playing."); } //implementation of stop abstract method 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(); //the MP3 Player is playing. //error: MusicPlayer does not contain pause() method. //musicPlayer.pause(); } }
Multiple inheritance by interface
Java does not support multiple inheritance which means that a class cannot extend more than one class.
But, multiple inheritance using interfaces is possible. We can implement multiple Interfaces in a single class.
A class implementing Interfaces must implement all the abstract methods of all the Interfaces.
Example
//Multiple inheritance by interface example. interface MusicPlayer { //play() and stop() are abstract methods void play(); void stop(); } interface VideoPlayer { //changeContrast() and changeBrightness() are abstract methods void changeContrast(int x); void changeBrightness(int x); } //Smartphone class implementing interfaces MusicPlayer,VideoPlayer class Smartphone implements MusicPlayer, VideoPlayer { //implementation of play abstract method public void play() { System.out.println("playing..."); } //implementation of stop abstract method public void stop() { System.out.println("stopped..."); } //implementation of changeContrast abstract method public void changeContrast(int x) { System.out.println("Constrast Changed by: " + x); } //implementation of changeBrightness abstract method 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); } }
Output
playing... Brightness Changed by: 20
Interface Default methods
Before Java 8, interface contains only abstract methods. The implementation of these methods has to be provided in a class which implements the interface.
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.
To overcome this issue, Java 8 now provides us default methods for Interfaces.
Default methods allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
If a class does not implement a default method, the compiler will take the implementation mentioned within the Interface.
To declare default methods inside interfaces, we use the default keyword.
Example
//Interface default methods example interface MusicPlayer { //play() and stop() are abstract methods void play(); void stop(); //default method default void setVolume(int x) { System.out.println("Player volume Changed by: " + x); } } //MP3Player class implementing MusicPlayer interface class MP3Player implements MusicPlayer { //implementation of play abstract method public void play() { System.out.println("the MP3 Player is playing."); } //implementation of stop abstract method public void stop() { System.out.println("the MP3 Player is off."); } //implementation of setVolume default method 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(); //setVolume default method defined in MP3Player class get executed mp3Player.setVolume(110); } }
Output
the MP3 Player is playing. Player volume level should be less then 100.
Default Methods and Multiple Inheritance
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.
Example
//multiple inheritance through default methods example. interface MusicPlayer { //default method default void next() { System.out.println("Next from MusicPlayer"); } } interface VideoPlayer { //default method default void next() { System.out.println("Next from VideoPlayer"); } } //Smartphone class implementing MusicPlayer and VideoPlayer interface class Smartphone implements MusicPlayer, VideoPlayer { //overriding default next() method public void next() { //use super keyword to call the next //method of MusicPlayer interface MusicPlayer.super.next(); //use super keyword to call the next //method of VideoPlayer interface VideoPlayer.super.next(); } } class Main { public static void main(String[] args) { Smartphone smartphone = new Smartphone(); //default next() method executed smartphone.next(); } }
Output
Next from MusicPlayer Next from VideoPlayer
Interface static methods
Java 8 also provides the ability to add static methods to interfaces.
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.
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.
Example
//Interface static method example interface MusicPlayer { //static method public static void print() { System.out.println("MusicPlayer static method called.."); } //abstract method void play(); } //MP3Player class implementing MusicPlayer interface class MP3Player implements MusicPlayer { //implementation of play abstract method 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(); // static method executed // mp3Player.print(); // This will cause a compilation error } }
Output
MusicPlayer static method called..
Inheriting an Interface
An interface can extend another interface or interfaces (more than one interface) .
Any class that implements an interface must implement the abstract methods declared in that interface plus all the abstract methods that are present in the super interface.
In Java, extends keyword is used for extending interfaces.
Example
//Inheriting an interface example. interface Player { //abstract methods play() and stop() public void play(); public void stop(); } //VideoPlayer extending Player interface interface VideoPlayer extends Player { //abstract method void changeBrightness(int x); //default method default public void next() { System.out.println("Next from VideoPlayer"); } } //class must implement abstract methods of VideoPlayer and Player class SmartPhone implements VideoPlayer { //implementation of play abstract method public void play() { System.out.println("video playing.."); } //implementation of stop abstract method public void stop() { System.out.println("video stopped.."); } //implementation of changeBrightness abstract method 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); } }
Output
video playing.. Brightness Changed by: 20
Points to Remember:
- Interface contains methods (implicitly public abstract) and fields (implicitly public static final).
- In Java 8 and above, Interfaces can have default methods and static methods.
- An Interface cannot be instantiated but we can create reference variable of it that refers to the object of its implemented class.
- A class that implements interface must provide implementation to all the abstract methods of interface or you have to declare it as abstract class.
- Interface do not have constructors.
- To inherit an interface within a class we need to use implements keyword and to inherit an interface in another interface we need to use extends keyword.
- A class can implement more than one interface.
- An interface can extends more than one interface.
- Java does not support multiple inheritance using class, but by using interface it is possible.
- Interface is used to achieve abstraction and loose coupling.