Provide Best Programming Tutorials

Interface In Java

What is an interface in Java?

Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body, see: Java abstract method). Also, the variables declared in an interface are public, static & final by default. We will cover this in detail, later in this guide.

What is the use of interface in Java?

As mentioned above they are used for full abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.

Syntax: Interfaces are declared by specifying a keyword “interface”. E.g.:

interface MyInterface{
   /* All the methods are public abstract by default
    * As you see they have no body
    */
   public void method1();
   public void method2();
}

Example of an Interface in Java

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface or in other words you can say that class has to implement all the methods of interface.

interface MyInterface{
   /* compiler will treat them as: 
    * public abstract void method1();
    * public abstract void method2();
    */
   public void method1();
   public void method2();
}
class Demo implements MyInterface{
   /* This class must have to implement both the abstract methods
    * else you will get compilation error
    */
   public void method1(){
    System.out.println("implementation of method1");
   }
   public void method2(){
    System.out.println("implementation of method2");
   }
   public static void main(String arg[]){
    MyInterface obj = new Demo();
    obj.method1();
   }
}

Tag or Marker interface in Java

An empty interface is known as tag or marker interface. For example Serializable, EventListener, Remote(java.rmi.Remote) are tag interfaces. These interfaces do not have any field and methods in it.

For example Serializable, EventListener, Remote(java.rmi.Remote) are tag interfaces, there are few other tag interfaces as well.

These interfaces do not have any field and methods in it. You must be thinking if they are empty why class implements them? What’s the use of it? Class implements them to claim the membership in a particular set. For example: If a class implements Serializable interface, it is claiming to be the member of Serializable classes, so if JVM (Java Virtual Machine) sees that a class is Serializable, it does some trick or special operation that helps in the serialization/de-serialization process.

Basically Tag interfaces are meaningful to the JVM (Java virtual machine). You can also create your own tag interfaces to segregate and categorize your code. It would improve the readability of your code.

This is how a tag interface looks:

package java.util;
public interface EventListener{}

Leave a Reply

Close Menu