Java Interfaces Explained with Program Examples

An interface in java is a mechanism to achieve abstraction. Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body). It is used to achieve abstraction and multiple inheritance in Java.

Java Interface also represents IS-A relationship.

Some Important Points to remember about Java Interfaces –

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.
  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.
  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
  • We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it that refers to the Object of its implementing class.
How to Declare Interfaces  ?

Interface is declared by using interface keyword. It provides total abstraction; means all the methods in interface are declared with empty body and are public and all fields are public, static and final by default. A class that implement interface must implement all the methods declared in the interface.

interface <interface_name>{  
    // declare constant fields  
    // declare methods that abstract   
    // by default.  
}
Internal addition by compiler –

The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members. In other words, Interface fields are public, static and final by default, and methods are public and abstract.

java interface compilation process

Relation between Class and Interface –

As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.

java class and interface relationship

Java Interfaces Program Example –
interface Bank {
    float rateOfInterest();
}

class SBI implements Bank {
    @Override
    public float rateOfInterest() {
        return 9.15f;
    }
}
class ICICI implements Bank {

    @Override
    public float rateOfInterest() {
        return 9.7f;
    }
}

public class JavaApplication4 {

    public static void main(String[] args) {

        Bank b = new SBI();
        System.out.println("ROI: " + b.rateOfInterest());

    }
}
Output
ROI: 9.15
Multiple Inheritance in Java –

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

multiple inheritance in java using interfaces

Java 8 Default Method in Interface –

Since Java 8, we can have method body in interface. But we need to make it default method. Let’s see an example:

void draw();  
default void msg(){System.out.println("default method");}  
}  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
class TestInterfaceDefault{  
public static void main(String args[]){  
Drawable d=new Rectangle();  
d.draw();  
d.msg();  
}
}
Output
drawing rectangle
default method
Java 8 Static Method in Interface –

Since Java 8, we can have static method in interface. Let’s see an example:

interface Drawable{  
void draw();  
static int cube(int x){return x*x*x;}  
}  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
  
class TestInterfaceStatic{  
public static void main(String args[]){  
System.out.println(Drawable.cube(3));  
}
}
Output
27

Leave a Reply

Your email address will not be published. Required fields are marked *