Java Reference
In-Depth Information
}
class Test {
public static void main(String[] args) throws IOException {
LineBufferOutput lbo = new LineBufferOutput(System.out);
lbo.putstr("lbo\nlbo");
System.out.print("print\n");
lbo.putstr("\n");
}
}
This program produces the output:
lbo
print
lbo
The class BufferOutput implements a very simple buffered version of an OutputStream ,
flushing the output when the buffer is full or flush is invoked. The subclass LineBuffer-
Output declares only a constructor and a single method putchar , which overrides the
method putchar of BufferOutput . It inherits the methods putstr and flush from class Buffer-
Output .
In the putchar method of a LineBufferOutput object, if the character argument is a
newline, then it invokes the flush method. The critical point about overriding in this
example is that the method putstr , which is declared in class BufferOutput , invokes the
putchar method defined by the current object this , which is not necessarily the putchar
method declared in class BufferOutput .
Thus, when putstr is invoked in main using the LineBufferOutput object lbo , the invocation
of putchar in the body of the putstr method is an invocation of the putchar of the object
lbo , the overriding declaration of putchar that checks for a newline. This allows a sub-
class of BufferOutput to change the behavior of the putstr method without redefining it.
Documentation for a class such as BufferOutput , which is designed to be extended,
should clearly indicate what is the contract between the class and its subclasses, and
should clearly indicate that subclasses may override the putchar method in this way.
The implementor of the BufferOutput class would not, therefore, want to change the im-
plementation of putstr in a future implementation of BufferOutput not to use the meth-
od putchar , because this would break the pre-existing contract with subclasses. See the
discussion of binary compatibility in §13, especially § 13.2 .
8.4.8.2. Hiding (by Class Methods)
If a class declares a static method m , then the declaration m is said to hide any method m' ,
where the signature of m is a subsignature (§ 8.4.2 ) of the signature of m' , in the super-
Search WWH ::




Custom Search