Interface in Java

Beginners code Computer Skills Java Mobile Development

What is an Interface in Java:

  • Interface keyword is used to design any user-defined or predefined interface.
  • It is very similar to an abstract class. And both abstract classes and interfaces support the concept of abstraction.
  • The interface in java supports 100% abstraction. It has only static constants and abstract methods. It cannot contain a concrete method.
  • The Java compiler converts methods of interface as public and abstract, data members as public, static and final by default.
  • Similar to abstract classes, interfaces also cannot be instantiated.

Reasons to use interface:

  • It helps in achieving full abstraction.
  • With the help of the interface, Java supports the functionality of multiple inheritances.

Difference between abstract class and interface:

Abstract class Interface
Supports 0 to 100% abstraction.Support 100% abstraction.
Abstract class may contain any abstract or concrete method.Interface only contains abstract methods. By default the method within interface is public and abstract.
Abstract classes can have a body.Interfaces cannot have a body.
Any type of variable is allowed within abstract class.The variable is public, static and final if it is declared inside an interface.
User can design a constructor within abstract class.Constructors are not allowed in interface.
Abstract class doesn't support multiple inheritance.Interface directly supports multiple inheritance.

Multiple inheritance by Interface:

  • A class cannot extend multiple classes in Java, but it can implement multiple interfaces easily.
public interface Showable {

  void show();
}
public interface Printable {

  void print(); 
}
public class TestShowPrint implements Printable, Showable {

  public void print() {
    System.out.println("Print method");
  }
  
  public void show() {
    System.out.println("Show method");
  }
  
  public static void main(String[] args) {
    TestShowPrint obj = new TestShowPrint();
    obj.print();
    obj.show();		
  }
}

Output:

Printable interface

A class implements an interface but an interface extends another interface. However, an interface can also extend multiple interfaces.

public interface PrintClass {

  void print(); 
}
public interface ShowClass {

  void show();
}
public class DemoPrintShow implements PrintClass, ShowClass {
  
  public void print() {
    System.out.println("Print method");
  }
  
  public void show() {
    System.out.println("Show method");
  }
  
  public static void main(String[] args) {
    TestShowPrint obj = new TestShowPrint();
    obj.print();
    obj.show();
    
  }
}

Output:

interface extends interface

Marker Interface:

  • An interface that does not have any members is known as a marker or tag interface. In other words, it is an empty interface.
  • For example, import java.lang.Cloneable;, import java.io.Serializable;, import java.util.EventListener;
  • They only provide some essential information to the JVM so that JVM may perform some useful operation.
public class StudentMarker implements Cloneable {
  int rollno;
  String name;
  
  public StudentMarker(int rollno, String name) {
        
    this.name = name;
    this.rollno = rollno;
  }
  
  public Object clone() throws CloneNotSupportedException{
    return super.clone();
  }

  public static void main(String[] args) throws CloneNotSupportedException {
    StudentMarker obj1 = new StudentMarker(103, "Satya");
    System.out.println(obj1.rollno + " " + obj1.name);
    
    StudentMarker obj2 = (StudentMarker)obj1.clone();
    System.out.println(obj2.rollno+" " + obj2.name);
  }
}

Output:

Marker interface

The Nested Interface:

An interface can have another interface inside it. This is the concept of a nested interface.

public interface Message {
  void show();  
    
  interface ShowMessage {  
     void msg();  
  }    
}
public class NestedMessage implements Message.ShowMessage, Message {
  
  public void msg() {
    System.out.println("Nested interface msg method !");        
  }
    
  public void show() {
    System.out.println("Outer interface show method!");
  }
  public static void main(String args[]){  
    Message.ShowMessage obj = new NestedMessage();
    obj.msg();

    Message obj2 = new NestedMessage();
    obj2.show();
   }
}

Output:

nested interface