0% found this document useful (0 votes)
57 views

Spring Java-Interfaces

The document discusses interfaces in Java. It defines an interface as a declaration that can be implemented by classes to provide a common supertype without requiring a shared superclass. Interfaces have no instance fields or implementations of methods, but classes that implement interfaces must provide implementations of all abstract methods declared in the interfaces. The document outlines different types of interfaces like top-level, nested, annotation, and generic interfaces, and rules regarding interface inheritance and extending other interfaces.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Spring Java-Interfaces

The document discusses interfaces in Java. It defines an interface as a declaration that can be implemented by classes to provide a common supertype without requiring a shared superclass. Interfaces have no instance fields or implementations of methods, but classes that implement interfaces must provide implementations of all abstract methods declared in the interfaces. The document outlines different types of interfaces like top-level, nested, annotation, and generic interfaces, and rules regarding interface inheritance and extending other interfaces.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

C H A P T E R 9

Interfaces

AN interface declaration defines a new interface that can be implemented by one


or more classes. Programs can use interfaces to provide a common supertype for
otherwise unrelated classes, and to make it unnecessary for related classes to share
a common abstract superclass.
Interfaces have no instance variables, and typically declare one or more abstract
methods; otherwise unrelated classes can implement an interface by providing
implementations for its abstract methods. Interfaces may not be directly
instantiated.
A top level interface (§7.6) is an interface declared directly in a compilation unit.
A nested interface is any interface whose declaration occurs within the body of
another class or interface declaration. A nested interface may be a member interface
(§8.5, §9.5) or a local interface (§14.3).
An annotation interface (§9.6) is an interface declared with distinct syntax,
intended to be implemented by reflective representations of annotations (§9.7).
This chapter discusses the common semantics of all interfaces. Details that are
specific to particular kinds of interfaces are discussed in the sections dedicated to
these constructs.
An interface may be declared to be a direct extension of one or more other
interfaces, meaning that it inherits all the member classes and interfaces, instance
methods, and static fields of the interfaces it extends, except for any members
that it may override or hide.
A class may be declared to directly implement one or more interfaces (§8.1.5),
meaning that any instance of the class implements all the abstract methods
specified by the interface or interfaces. A class necessarily implements all the
interfaces that its direct superclasses and direct superinterfaces do. This (multiple)

341
9.1 Interface Declarations INTERFACES

interface inheritance allows objects to support (multiple) common behaviors


without sharing a superclass.
Unlike a class, an interface cannot be declared final. However, an interface may
be declared sealed (§9.1.1.4) to limit its subclasses and subinterfaces.
A variable whose declared type is an interface type may have as its value a
reference to any instance of a class which implements the specified interface. It is
not sufficient that the class happen to implement all the abstract methods of the
interface; the class or one of its superclasses must actually be declared to implement
the interface, or else the class is not considered to implement the interface.

9.1 Interface Declarations

An interface declaration specifies an interface.


There are two kinds of interface declarations: normal interface declarations and
annotation interface declarations (§9.6).

InterfaceDeclaration:
NormalInterfaceDeclaration
AnnotationInterfaceDeclaration

NormalInterfaceDeclaration:
{InterfaceModifier} interface TypeIdentifier [TypeParameters]
[InterfaceExtends] [InterfacePermits] InterfaceBody

The TypeIdentifier in an interface declaration specifies the name of the interface.


It is a compile-time error if an interface has the same simple name as any of its
enclosing classes or interfaces.
The scope and shadowing of an interface declaration is specified in §6.3 and §6.4.1.

9.1.1 Interface Modifiers


An interface declaration may include interface modifiers.

InterfaceModifier:
(one of)
Annotation public protected private
abstract static sealed non-sealed strictfp

342
INTERFACES Interface Declarations 9.1

The rules concerning annotation modifiers for an interface declaration are specified
in §9.7.4 and §9.7.5.
The access modifier public (§6.6) pertains only to top level interfaces (§7.6) and
member interfaces (§8.5, §9.5), not to local interfaces (§14.3).
The access modifiers protected and private pertain only to member interfaces.
The modifier static pertains only to member interfaces and local interfaces.
It is a compile-time error if the same keyword appears more than once as a modifier
for an interface declaration, or if a interface declaration has more than one of the
access modifiers public, protected, and private.
It is a compile-time error if an interface declaration has more than one of the
modifiers sealed and non-sealed.

If two or more (distinct) interface modifiers appear in an interface declaration, then it is


customary, though not required, that they appear in the order consistent with that shown
above in the production for InterfaceModifier.

9.1.1.1 abstract Interfaces


Every interface is implicitly abstract.

This modifier is obsolete and should not be used in new code.

9.1.1.2 strictfp Interfaces


The strictfp modifier on an interface declaration is obsolete and should not be
used in new code. Its presence or absence has no effect at compile time or run time.

9.1.1.3 static Interfaces


A nested interface is implicitly static. That is, every member interface and local
interface is static. It is permitted for the declaration of a member interface to
redundantly specify the static modifier (§9.5), but it is not permitted for the
declaration of a local interface (§14.3).
Because a nested interface is static, it has no immediately enclosing instance
(§8.1.3). References from a nested interface to type parameters, instance variables,
local variables, formal parameters, exception parameters, or instance methods
in lexically enclosing class, interface, or method declarations are disallowed
(§6.5.5.1, §6.5.6.1, §15.12.3).

343
9.1 Interface Declarations INTERFACES

9.1.1.4 sealed and non-sealed Interfaces


An interface can be declared sealed if all its direct subclasses and direct
subinterfaces are known when the interface is declared (§9.1.4), and no other direct
subclasses or direct subinterfaces are desired or required.

It is useful to recall that a class is said to be a direct subclass of its direct superinterfaces
(§8.1.5).

An interface is freely extensible if none of its direct superinterfaces are sealed


(§9.1.3), and it is not sealed itself.
An interface that has a sealed direct superinterface is freely extensible if and only
if it is declared non-sealed.
It is a compile-time error if an interface has a sealed direct superinterface and is
not declared sealed or non-sealed.
It is a compile-time error if an interface is declared non-sealed but has no sealed
direct superinterface.

9.1.2 Generic Interfaces and Type Parameters


An interface is generic if the interface declaration declares one or more type
variables (§4.4).
These type variables are known as the type parameters of the interface. The type
parameter section follows the interface name and is delimited by angle brackets.

The following productions from §8.1.2 and §4.4 are shown here for convenience:

TypeParameters:
< TypeParameterList >

TypeParameterList:
TypeParameter {, TypeParameter}

TypeParameter:
{TypeParameterModifier} TypeIdentifier [TypeBound]

TypeParameterModifier:
Annotation

TypeBound:
extends TypeVariable
extends ClassOrInterfaceType {AdditionalBound}

AdditionalBound:
& InterfaceType

344
INTERFACES Interface Declarations 9.1

The rules concerning annotation modifiers for a type parameter declaration are
specified in §9.7.4 and §9.7.5.
In an interface's type parameter section, a type variable T directly depends on a
type variable S if S is the bound of T, while T depends on S if either T directly
depends on S or T directly depends on a type variable U that depends on S (using this
definition recursively). It is a compile-time error if a type variable in a interface's
type parameter section depends on itself.
The scope and shadowing of an interface's type parameter is specified in §6.3 and
§6.4.1.
References to an interface's type parameter from a static context or a nested class
or interface are restricted, as specified in §6.5.5.1.
A generic interface declaration defines a set of parameterized types (§4.5), one for
each possible parameterization of the type parameter section by type arguments.
All of these parameterized types share the same interface at run time.

9.1.3 Superinterfaces and Subinterfaces


If an extends clause is provided, then the interface being declared extends each
of the specified interface types and therefore inherits the member classes, member
interfaces, instance methods, and static fields of each of those interface types.
The specified interface types are the direct superinterface types of the interface
being declared.
Any class that implements the declared interface is also considered to implement
all the interfaces that this interface extends.

InterfaceExtends:
extends InterfaceTypeList

The following production from §8.1.5 is shown here for convenience:

InterfaceTypeList:
InterfaceType {, InterfaceType}

Each InterfaceType in the extends clause of an interface declaration must name


an accessible interface (§6.6), or a compile-time error occurs.
It is a compile-time error if any InterfaceType names a interface that is sealed
(§9.1.1.4) and the interface being declared is not a permitted direct subinterface of
the named interface (§9.1.4).

345
9.9 Function Types INTERFACES

wildcard parameterization of I, I<T1...Tn>. The non-wildcard parameterization


is determined as follows.
Let P1...Pn be the type parameters of I with corresponding bounds B1...Bn. For all
i (1 ≤ i ≤ n), Ti is derived according to the form of Ai:
– If Ai is a type, then Ti = Ai.
– If Ai is a wildcard, and the corresponding type parameter's bound, Bi, mentions
one of P1...Pn, then Ti is undefined and there is no function type.
– Otherwise:
› If Ai is an unbound wildcard ?, then Ti = Bi.
› If Ai is a upper-bounded wildcard ? extends Ui, then Ti = glb(Ui, Bi)
(§5.1.10).
› If Ai is a lower-bounded wildcard ? super Li, then Ti = Li.
• The function type of the raw type of a generic functional interface I<...> is the
erasure of the function type of the generic functional interface I<...>.
• The function type of an intersection type that induces a notional functional
interface is the function type of the notional functional interface.

Example 9.9-1. Function Types

Given the following interfaces:

interface X { void m() throws IOException; }


interface Y { void m() throws EOFException; }
interface Z { void m() throws ClassNotFoundException; }

the function type of:

interface XY extends X, Y {}

is:

()->void throws EOFException

while the function type of:

interface XYZ extends X, Y, Z {}

is:

()->void (throws nothing)

396
INTERFACES Function Types 9.9

Given the following interfaces:

interface A {
List<String> foo(List<String> arg)
throws IOException, SQLTransientException;
}
interface B {
List foo(List<String> arg)
throws EOFException, SQLException, TimeoutException;
}
interface C {
List foo(List arg) throws Exception;
}

the function type of:

interface D extends A, B {}

is:

(List<String>)->List<String>
throws EOFException, SQLTransientException

while the function type of:

interface E extends A, B, C {}

is:

(List)->List throws EOFException, SQLTransientException

The function type of a functional interface is defined nondeterministically: while the


signatures in M are "the same", they may be syntactically different (HashMap.Entry and
Map.Entry, for example); the return type may be a subtype of every other return type, but
there may be other return types that are also subtypes (List<?> and List<? extends
Object>, for example); and the order of thrown types is unspecified. These distinctions
are subtle, but they can sometimes be important. However, function types are not used
in the Java programming language in such a way that the nondeterminism matters. Note
that the return type and throws clause of a "most specific method" are also defined
nondeterministically when there are multiple abstract methods (§15.12.2.5).

When a generic functional interface is parameterized by wildcards, there are many


different instantiations that could satisfy the wildcard and produce different function types.
For example, each of Predicate<Integer> (function type Integer -> boolean),
Predicate<Number> (function type Number -> boolean), and Predicate<Object>
(function type Object -> boolean) is a Predicate<? super Integer>. Sometimes, it
is possible to known from the context, such as the parameter types of a lambda expression,
which function type is intended (§15.27.3). Other times, it is necessary to pick one; in
these circumstances, the bounds are used. (This simple strategy cannot guarantee that the
resulting type will satisfy certain complex bounds, so not all complex cases are supported.)

397
9.9 Function Types INTERFACES

Example 9.9-2. Generic Function Types

A function type may be generic, as a functional interface's abstract method may be


generic. For example, in the following interface hierarchy:

interface G1 {
<E extends Exception> Object m() throws E;
}
interface G2 {
<F extends Exception> String m() throws Exception;
}
interface G extends G1, G2 {}

the function type of G is:

<F extends Exception> ()->String throws F

A generic function type for a functional interface may be implemented by a method


reference expression (§15.13), but not by a lambda expression (§15.27) as there is no syntax
for generic lambda expressions.

398

You might also like