Syntax error What are the modifiers allowed for methods in an Interface in java?

What are the modifiers allowed for methods in an Interface in java?



An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.

In Java 7

As of Java7 you can have only public, abstract as modifiers for the methods of an interface.

interface MyInterface{
   public abstract void display();
   public abstract void setName(String name);
   public abstract void setAge(int age);
}

Using any other modifier with the methods of an interface would lead to a compile time error.

From Java8

From Java8 onwards interfaces allow default methods and static methods.

  • Static methods − A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.
  • You need to call static method of an interface using the name of the interface.

Example

 Live Demo

interface MyInterface{
   public void demo();
   public static void display() {
      System.out.println("This is a static method");
   }
}
public class InterfaceExample{
   public void demo() {
      System.out.println("This is the implementation of the demo method");
   }
   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.demo();
      MyInterface.display();
   }
}

Output

This is the implementation of the demo method
This is a static method
  • Default methods − A default method is a default implementation of a method of an interface, if you have default method in an interface, there is no need to implement it in the classes that already implement this interface.
  • A default method is also known as defender method or virtual extension method. You can define a default method using the default keyword as −
default void display() {
   System.out.println("This is a default method");
}

Example

 Live Demo

interface sampleInterface{
   public void demo();
   default void display() {
      System.out.println("This is a default method");
   }
}
public class DefaultMethodExample implements sampleInterface{
   public void demo() {
      System.out.println("This is the implementation of the demo method");
   }
   public static void main(String args[]) {
      DefaultMethodExample obj = new DefaultMethodExample();
      obj.demo();
      obj.display();
   }
}

Output

This is the implementation of the demo method
This is a default method

From java 9

From Java9 onwards interfaces allow private and private static methods.

Example

interface MyInterface {
   public abstract void demo();
   public default void defaultMethod() {
      privateMethod();
      staticPrivateMethod();
      System.out.println("This is a default method of the interface");
   }

   public static void staticMethod() {
      staticPrivateMethod();
      System.out.println("This is a static method of the interface");
   }

   private void privateMethod(){
      System.out.println("This is a private method of the interface");
   }

   private static void staticPrivateMethod(){
      System.out.println("This is a static private method of the interface");
   }
}

public class InterfaceMethodsExample implements MyInterface {
   public void demo() {
      System.out.println("Implementation of the demo method");
   }

   public static void main(String[] args){
      InterfaceMethodsExample obj = new InterfaceMethodsExample();
      obj.defaultMethod();
      obj.demo();
      MyInterface.staticMethod();
   }
}

Output

This is a private method of the interface
This is a static private method of the interface
This is a default method of the interface
Implementation of the demo method
This is a static private method of the interface
This is a static method of the interface
Updated on: 2019-07-30T22:30:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements