Java Reference
In-Depth Information
}
abstract class InfiniteBuffer implements Buffer {
public abstract char get() throws BufferError;
}
The overriding declaration of method get in class InfiniteBuffer states that method get in
any subclass of InfiniteBuffer never throws a BufferEmpty exception, putatively because
it generates the data in the buffer, and thus can never run out of data.
Example 8.4.3.1-2. Abstract/Non-Abstract Overriding
We can declare an abstract class Point that requires its subclasses to implement toString
if they are to be complete, instantiable classes:
abstract class Point {
int x, y;
public abstract String toString();
}
This abstract declaration of toString overrides the non- abstract toString method of class Ob-
ject . (Class Object is the implicit direct superclass of class Point .) Adding the code:
Click here to view code image
class ColoredPoint extends Point {
int color;
public String toString() {
return super.toString() + ": color " + color; // error
}
}
results in a compile-time error because the invocation super.toString() refers to method
toString in class Point , which is abstract and therefore cannot be invoked. Method toString
of class Object can be made available to class ColoredPoint only if class Point explicitly
makes it available through some other method, as in:
Click here to view code image
abstract class Point {
int x, y;
public abstract String toString();
protected String objString() { return super.toString(); }
}
class ColoredPoint extends Point {
int color;
public String toString() {
return objString() + ": color " + color; // correct
Search WWH ::




Custom Search