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 §8.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.

Death, life, and sleep, reality and thought,
Assist me, God, their boundaries to know . . .
—William Wordsworth, Maternal Grief