13.4.4
Superclasses and Superinterfaces
BINARY COMPATIBILITY
cannot be assigned to a variable of type
Hyper
, because
Super
is not a subclass of
Hyper
.
It is instructive to consider what might happen without the verification step:
the program might run and print:
s
This demonstrates that without the verifier the type system could be defeated by
linking inconsistent binary files, even though each was produced by a correct Java
compiler.
As a further example, here is an implementation of a cast from a reference
type to
int
, which could be made to run in certain implementations of Java if they
failed to perform the verification process. Assume an implementation that uses
method dispatch tables and whose linker assigns offsets into those tables in a
sequential and straightforward manner. Then suppose that the following Java code
is compiled:
class Hyper { int zero(Object o) { return 0; } }
class Super extends Hyper { int peek(int i) { return i; } }
class Test extends Super {
public static void main(String[] args) throws Throwable {
Super as = new Super();
System.out.println(as);
System.out.println(Integer.toHexString(as.zero(as)));
}
}
The assumed implementation determines that the class
Super
has two methods:
the first is method
zero
inherited from class
Hyper
, and the second is the method
peek
. Any subclass of
Super
would also have these same two methods in the first
two entries of its method table. (Actually, all these methods would be preceded in
the method tables by all the methods inherited from class
Object
but, to simplify
the discussion, we ignore that here.) For the method invocation
as.zero(as)
, the
compiler specifies that the first method of the method table should be invoked; this
is always correct if type safety is preserved.
If the compiled code is then executed, it prints something like:
Super@ee300858
0
which is the correct output. But if a new version of
Super
is compiled, which is
the same except for the
extends
clause:
class Super { int peek(int i) { return i; } }
244
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved