---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 12 Edition"
java_version: 12
chapter: "Chapter 9. Interfaces"
section: "9.4"
heading: "9.4. Method Declarations"
parent: "Chapter 9. Interfaces"
source: https://docs.oracle.com/javase/specs/jls/se12/html/jls-9.html#jls-9.4
id: "9.4"
url: https://jvm.obfuscated.software/jls/se12/9.4.html
raw: https://jvm.obfuscated.software/jls/se12/9.4.md
---

# 9.4. Method Declarations

```
InterfaceMethodDeclaration:
  {InterfaceMethodModifier} MethodHeader MethodBody

InterfaceMethodModifier:
  (one of)
  Annotation public private
  abstract default static strictfp
```

The following productions from [§8.4](8.4.md), [§8.4.5](8.4.5.md), and [§8.4.7](8.4.7.md) are shown here for convenience:

```
MethodHeader:
  Result MethodDeclarator [Throws]
  TypeParameters {Annotation} Result MethodDeclarator [Throws]

Result:
  UnannType
  void

MethodDeclarator:
  Identifier ( [ReceiverParameter ,] [FormalParameterList] ) [Dims]

MethodBody:
  Block
  ;
```

The rules for annotation modifiers on an interface method declaration are specified in [§9.7.4](9.7.4.md) and [§9.7.5](9.7.5.md).

A method in the body of an interface may be declared `public` or `private` ([§6.6](6.6.md)). If no access modifier is given, the method is implicitly `public`. It is permitted, but discouraged as a matter of style, to redundantly specify the `public` modifier for a method declaration in an interface.

A *default method* is an instance method declared in an interface with the `default` modifier. Its body is always represented by a block, which provides a default implementation for any class that implements the interface without overriding the method. Default methods are distinct from concrete methods ([§8.4.3.1](8.4.3.1.md)), which are declared in classes, and from `private` interface methods, which are neither inherited nor overridden.

An interface can declare `static` methods, which are invoked without reference to a particular object. `static` interface methods are distinct from default methods, which are instance methods.

It is a compile-time error to use the name of a type parameter of any surrounding declaration in the header or body of a `static` method of an interface.

The effect of the `strictfp` modifier is to make all `float` or `double` expressions within the body of a default or `static` method be explicitly FP-strict ([§15.4](15.4.md)).

An interface method lacking a `private`, `default`, or `static` modifier is implicitly `abstract`. Its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the `abstract` modifier for such a method declaration.

Note that an interface method may not be declared with `protected` or package access, or with the modifiers `final`, `synchronized`, or `native`.

It is a compile-time error if the same keyword appears more than once as a modifier for an interface method declaration, or if an interface method declaration has more than one of the access modifiers `public` and `private` ([§6.6](6.6.md)).

It is a compile-time error if an interface method declaration has more than one of the keywords `abstract`, `default`, or `static`.

It is a compile-time error if an interface method declaration that contains the keyword `private` also contains the keyword `abstract` or `default`. It is permitted for an interface method declaration to contain both `private` and `static`.

It is a compile-time error if an interface method declaration that contains the keyword `abstract` also contains the keyword `strictfp`.

It is a compile-time error for the body of an interface to declare, explicitly or implicitly, two methods with override-equivalent signatures ([§8.4.2](8.4.2.md)). However, an interface may inherit several `abstract` methods with such signatures ([§9.4.1](9.4.1.md)).

A method declared in an interface may be generic. The rules for type parameters of a generic method in an interface are the same as for a generic method in a class ([§8.4.4](8.4.4.md)).

## 9.4.1. Inheritance and Overriding

An interface I *inherits* from its direct superinterfaces all `abstract` and default methods `m` for which all of the following are true:

- `m` is a member of a direct superinterface, J, of I.
- No method declared in I has a signature that is a subsignature ([§8.4.2](8.4.2.md)) of the signature of `m`.
- There exists no method `m`' that is a member of a direct superinterface, J', of I (`m` distinct from `m`', J distinct from J'), such that `m`' overrides from J' the declaration of the method `m`.

Note that methods are overridden on a signature-by-signature basis. If, for example, an interface declares two `public` methods with the same name ([§9.4.2](9.4.2.md)), and a subinterface overrides one of them, the subinterface still inherits the other method.

The third clause above prevents a subinterface from re-inheriting a method that has already been overridden by another of its superinterfaces. For example, in this program:

```
interface Top {
    default String name() { return "unnamed"; }
}
interface Left extends Top {
    default String name() { return getClass().getName(); }
}
interface Right extends Top {}

interface Bottom extends Left, Right {}
```

`Right` inherits `name()` from `Top`, but `Bottom` inherits `name()` from `Left`, not `Right`. This is because `name()` from `Left` overrides the declaration of `name()` in `Top`.

An interface does not inherit `private` or `static` methods from its superinterfaces.

If an interface I declares a `private` or `static` method `m`, and the signature of `m` is a subsignature of a `public` instance method `m`' in a superinterface of I, and `m`' would otherwise be accessible to code in I, then a compile-time error occurs.

In essence, a `static` method in an interface cannot hide an instance method in a superinterface. This is similar to the rule in [§8.4.8.2](8.4.8.2.md) whereby a `static` method in a class cannot hide an instance method in a superclass or superinterface. Note that the rule in [§8.4.8.2](8.4.8.2.md) speaks of a class that "declares or inherits a `static` method", whereas the rule above speaks only of an interface that "declares a `static` method", since an interface cannot inherit a `static` method. Also note that the rule in [§8.4.8.2](8.4.8.2.md) allows hiding of both instance and `static` methods in superclasses/superinterfaces, whereas the rule above considers only `public` instance methods in superinterfaces.

Along the same lines, a `private` method in an interface cannot override an instance method - whether `public` or `private` - in a superinterface. This is similar to the rules in [§8.4.8.1](8.4.8.1.md) and [§8.4.8.3](8.4.8.3.md) whereby a `private` method in a class cannot override any instance method in a superclass or superinterface, because [§8.4.8.1](8.4.8.1.md) requires the overridden method to be non-`private` and [§8.4.8.3](8.4.8.3.md) requires the overriding method to provide at least as much access as the overridden method. In summary, only `public` methods in interfaces can be overridden, and only by `public` methods in subinterfaces or in implementing classes.

### 9.4.1.1. Overriding (by Instance Methods)

An instance method `m_I` declared in or inherited by interface I, *overrides from I* another instance method `m_J` declared in interface J, iff all of the following are true:

- I is a subinterface of J.
- I does not inherit `m_J`.
- The signature of `m_I` is a subsignature ([§8.4.2](8.4.2.md)) of the signature of `m_J`.
- `m_J` is `public`.

The presence or absence of the `strictfp` modifier has absolutely no effect on the rules for overriding methods. For example, it is permitted for a method that is not FP-strict to override an FP-strict method and it is permitted for an FP-strict method to override a method that is not FP-strict.

An overridden default method can be accessed by using a method invocation expression ([§15.12](15.12.md)) that contains the keyword `super` qualified by a superinterface name.

### 9.4.1.2. Requirements in Overriding

The relationship between the return type of an interface method and the return types of any overridden interface methods is specified in [§8.4.8.3](8.4.8.3.md).

The relationship between the `throws` clause of an interface method and the `throws` clauses of any overridden interface methods is specified in [§8.4.8.3](8.4.8.3.md).

The relationship between the signature of an interface method and the signatures of any overridden interface methods is specified in [§8.4.8.3](8.4.8.3.md).

The relationship between the accessibility of an interface method and the accessibility of any overridden interface methods is specified in [§8.4.8.3](8.4.8.3.md).

It is a compile-time error if a default method is override-equivalent with a non-`private` method of the class `Object`, because any class implementing the interface will inherit its own implementation of the method.

The prohibition against declaring one of the `Object` methods as a default method may be surprising. There are, after all, cases like `java.util.List` in which the behavior of `toString` and `equals` are precisely defined. The motivation becomes clearer, however, when some broader design decisions are understood:

- First, methods inherited from a superclass are allowed to override methods inherited from superinterfaces ([§8.4.8.1](8.4.8.1.md)). So, every implementing class would automatically override an interface's `toString` default. This is longstanding behavior in the Java programming language. It is not something we wish to change with the design of default methods, because that would conflict with the goal of allowing interfaces to unobtrusively evolve, only providing default behavior when a class doesn't already have it through the class hierarchy.
- Second, interfaces do *not* inherit from `Object`, but rather implicitly declare many of the same methods as `Object` ([§9.2](9.2.md)). So, there is no common ancestor for the `toString` declared in `Object` and the `toString` declared in an interface. At best, if both were candidates for inheritance by a class, they would conflict. Working around this problem would require awkward commingling of the class and interface inheritance trees.
- Third, use cases for declaring `Object` methods in interfaces typically assume a linear interface hierarchy; the feature does not generalize very well to multiple inheritance scenarios.
- Fourth, the `Object` methods are so fundamental that it seems dangerous to allow an arbitrary superinterface to silently add a default method that changes their behavior.

An interface is free, however, to define another method that provides behavior useful for classes that override the `Object` methods. For example, the `java.util.List` interface could declare an `elementString` method that produces the string described by the contract of `toString`; implementors of `toString` in classes could then delegate to this method.

### 9.4.1.3. Inheriting Methods with Override-Equivalent Signatures

It is possible for an interface to inherit several methods with override-equivalent signatures ([§8.4.2](8.4.2.md)).

If an interface I inherits a default method whose signature is override-equivalent with another method inherited by I, then a compile-time error occurs. (This is the case whether the other method is `abstract` or `default`.)

Otherwise, all the inherited methods are `abstract`, and the interface is considered to inherit all the methods.

One of the inherited methods must be return-type-substitutable for every other inherited method, or else a compile-time error occurs. (The `throws` clauses do not cause errors in this case.)

There might be several paths by which the same method declaration is inherited from an interface. This fact causes no difficulty and never, of itself, results in a compile-time error.

Naturally, when two different default methods with matching signatures are inherited by a subinterface, there is a behavioral conflict. We actively detect this conflict and notify the programmer with an error, rather than waiting for the problem to arise when a concrete class is compiled. The error can be avoided by declaring a new method that overrides, and thus prevents the inheritance of, all conflicting methods.

Similarly, when an abstract method and a default method with matching signatures are inherited by a subinterface, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).

In contrast, the longstanding behavior for inherited concrete methods in classes is that they override abstract methods declared in interfaces (see [§8.4.8](8.4.8.md)). The same argument about potential contract violation applies here, but in this case there is an inherent imbalance between classes and interfaces. We prefer, in order to preserve the independent nature of class hierarchies, to minimize class-interface clashes by simply giving priority to concrete methods.

## 9.4.2. Overloading

If two methods of an interface (whether both declared in the same interface, or both inherited by an interface, or one declared and one inherited) have the same name but different signatures that are not override-equivalent ([§8.4.2](8.4.2.md)), then the method name is said to be *overloaded*.

This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the `throws` clauses of two methods with the same name but different signatures that are not override-equivalent.

**Example 9.4.2-1. Overloading an `abstract` Method Declaration**

```
interface PointInterface {
    void move(int dx, int dy);
}
interface RealPointInterface extends PointInterface {
    void move(float dx, float dy);
    void move(double dx, double dy);
}
```

Here, the method named `move` is overloaded in interface `RealPointInterface` with three different signatures, two of them declared and one inherited. Any non-`abstract` class that implements interface `RealPointInterface` must provide implementations of all three method signatures.

## 9.4.3. Interface Method Body

A default method has a block body. This block of code provides an implementation of the method in the event that a class implements the interface but does not provide its own implementation of the method.

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

It is a compile-time error if an interface method declaration is `abstract` (explicitly or implicitly) and has a block for its body.

It is a compile-time error if an interface method declaration is `default`, `private`, or `static`, and has a semicolon for its body.

It is a compile-time error for the body of a `static` method to attempt to reference the current object using the keyword `this` or the keyword `super`.

The rules for `return` statements in a method body are specified in [§14.17](14.17.md).

If a method is declared to have a return type ([§8.4.5](8.4.5.md)), then a compile-time error occurs if the body of the method can complete normally ([§14.1](14.1.md)).
