CHAPTER 9: Interface Declarations Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

9.4 Abstract Method Declarations

9.4.1 Inheritance and Overriding , 9.4.2 Overloading , 9.4.3 Examples of Abstract Method Declarations


AbstractMethodDeclaration:

	AbstractMethodModifiersopt ResultType MethodDeclarator Throwsopt ;

AbstractMethodModifiers:

	AbstractMethodModifier

	AbstractMethodModifiers AbstractMethodModifier

AbstractMethodModifier: one of

	public abstract

The access modifier public is discussed in S6.6. A compile-time error occurs if the same modifier appears more than once in an abstract method declaration.

Every method declaration in the body of an interface is implicitly abstract , so its body is always represented by a semicolon, not a block. For compatibility with older versions of Java, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.

Every method declaration in the body of an interface is implicitly public . It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.

Note that a method declared in an interface must not be declared static , or a compile-time error occurs, because in Java static methods cannot be abstract .

Note that a method declared in an interface must not be declared native or synchronized , or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared native or synchronized in a class that implements the interface.

Note that a method declared in an interface must not be declared final or a compile-time error occurs. However, a method declared in an interface may be implemented by a method that is declared final in a class that implements the interface.


9.4.1 Inheritance and Overriding

If the interface declares a method, then the declaration of that method is said to override any and all methods with the same signature in the superinterfaces of the interface that would otherwise be accessible to code in this interface.

If a method declaration in an interface overrides the declaration of a method in another interface, a compile-time error occurs if the methods have different return types or if one has a return type and the other is void . Moreover, a method declaration must not have a throws clause that conflicts (S8.4.4) with that of any method that it overrides; otherwise, a compile-time error occurs.

Methods are overridden on a signature-by-signature basis. If, for example, an interface declares two public methods with the same name, and a subinterface overrides one of them, the subinterface still inherits the other method.

An interface inherits from its direct superinterfaces all methods of the superinterfaces that are not overridden by a declaration in the interface.

It is possible for an interface to inherit more than one method with the same signature (S8.4.2). Such a situation does not in itself cause a compile-time error. The interface is considered to inherit all the methods. However, a compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void . (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.


9.4.2 Overloading

If two methods of an interface (whether both declared in the same interface, or both inherited by a interface, or one declared and one inherited) have the same name but different signatures, 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.


9.4.3 Examples of Abstract Method Declarations

The following examples illustrate some (possibly subtle) points about abstract method declarations.

9.4.3.1 Example: Overriding

Methods declared in interfaces are abstract and thus contain no implementation. About all that can be accomplished by an overriding method declaration, other than to affirm a method signature, is to restrict the exceptions that might be thrown by an implementation of the method. Here is a variation of the example shown in S8.4.3.1:


class BufferEmpty extends Exception {
	BufferEmpty() { super(); }
	BufferEmpty(String s) { super(s); }
}


class BufferError extends Exception {
	BufferError() { super(); }
	BufferError(String s) { super(s); }
}


public interface Buffer {
	char get() throws BufferEmpty, BufferError;
}


public interface InfiniteBuffer extends Buffer {
	 char get() throws BufferError;												// override
}


9.4.3.2 Example: Overloading

In the example code:


interface PointInterface {
	void move(int dx, int dy);
}


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

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

Top© 1996 Sun Microsystems, Inc. All rights reserved.