Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an interface method have a body?

People also ask

Why interface has no method body?

A Java interface contains a list of methods that must be implemented by the class that implements the interface. Thus, the methods have no body: the body of each method is in the implementing class(es).

Which method Cannot have a body in Java?

Abstract methods do not have the body they only have declaration but no definition. The definition is defined by implementing classes.

Can an interface have a static method body?

Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.

Which Member methods of the interface can have their body?

The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).


From Java 8 you can define static methods in interfaces in addition to default methods.

  • A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

  • This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.

  • The following example defines a static method that retrieves a ZoneId object corresponding to a time zone identifier; it uses the system default time zone if there is no ZoneId object corresponding to the given identifier. (As a result, you can simplify the method getZonedDateTime)

Here is code :

public interface TimeClient {
   // ...
    static public ZoneId getZoneId (String zoneString) {
        try {
            return ZoneId.of(zoneString);
        } catch (DateTimeException e) {
            System.err.println("Invalid time zone: " + zoneString +"; using default time zone instead.");
            return ZoneId.systemDefault();
        }
    }

   default public ZonedDateTime getZonedDateTime(String zoneString) {
      return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
   }    
}

See also

  • Oracle docs for interface methods

  • For all interesting things in Java 8 read Everything about Java 8


This is only possible in Java 8. In the Java 7 Language Specification §9.4, it explicitly states:

It is a compile-time error if a method declared in an interface is declared static, because static methods cannot be abstract.

So in Java 7, static methods in interfaces cannot exist.

If you go to the Java 8 Language Specification §9.4.3, you can see that it says:

A static method also has a block body, which provides the implementation of the method.

So it explicitly states that in Java 8, they can exist.

I even tried to run your exact code in Java 1.7.0_45, but it gave me the error "modifier static not allowed here".


Here is a quote directly from the Java 8 tutorial, Default Methods (Learning the Java Language > Interfaces and Inheritance):

Static Methods

In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class. The following example defines a static method that retrieves a ZoneId object corresponding to a time zone identifier; it uses the system default time zone if there is no ZoneId object corresponding to the given identifier. (As a result, you can simplify the method getZonedDateTime):

public interface TimeClient {
    // ...
    static public ZoneId getZoneId (String zoneString) {
        try {
            return ZoneId.of(zoneString);
        } catch (DateTimeException e) {
            System.err.println("Invalid time zone: " + zoneString +
                "; using default time zone instead.");
            return ZoneId.systemDefault();
        }
    }

    default public ZonedDateTime getZonedDateTime(String zoneString) {
        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    }    
}

Like static methods in classes, you specify that a method definition in an interface is a static method with the static keyword at the beginning of the method signature. All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.


For java version 7 or below, similar functionally you can achieve using nested class declared within interface body. and this nested class implements outer interface.

Example:

interface I1{
    public void doSmth();

    class DefaultRealizationClass implements  I1{

        @Override
        public void doSmth() {
           System.out.println("default realization");
        }
    }
}

How do we use it in our code?

class MyClass implements I1{

    @Override
    public void doSmth() {
         new I1.DefaultRealizationClass().doSmth();
    }   
}

Therefore default implementation encapsulated within interface.