|
|
Language Fundamentals - Interface Declarations
Syntax (JJ pg142)
modifiers interface InterfaceName extendsClause {
// Interface body
}
The modifiers and extendsClause are optional.
A compile time error occurs if an interface has a simple name the same as any of it's enclosing classes or interfaces (JLS §9.1)
Modifiers (JLS §9.1.1)
public protected private
abstract static strictfp
| Note |
- top-level interfaces may only be declared public
private interface A {} // compile error
protected interface B{} // compile error
|
extendsClause
- consists of the extends keyword followed by a comma separated list of the interfaces being extended.
| Note |
- Classes are based on single-inheritance, they can only extend one class.
Interfaces are allowed multiple-inheritance, they can extend more than one interface.
interface InterfaceA extends
interfaceX, interfaceY, ... {}
|
Interface body
- an interface body may contain constant declarations, abstract method declarations, inner classes and inner interfaces
- fields in an interface are implicitly static and final ie they MUST be constants (JLS§9.3)
- methods in an interface are implicitly abstract and public; they CANNOT be static (JLS§9.4)
- methods cannot be declared strictfp, native or synchronized (JLS§9.4)
- member classes declared in an interface are implicitly public and static (JLS§9.5)
Code Examples
Traps
- an interface method declared as native or synchronized
- an interface method declared as static
|