Other than being implemented in native code, native methods are like all other methods: they can be
overloaded, overridden, final, static, synchronized, public, protected, or private. A
native method cannot, however, be declared abstract or strictfp.
If you use a native method, all portability and safety of the code are lost. You cannot, for instance, use a
native method in almost any code you expect to download and run from across a network connection (an
applet, for example). The downloading system may or may not be of the same architecture, and even if it is, it
might not trust your system well enough to run arbitrary native code.
Native methods are implemented using an API provided by the people who wrote the virtual machine on
which the code executes. The standard one for C programmers is called JNIJava Native Interface. Others are
being defined for other native languages. A description of these APIs is beyond the scope of this book.
The significant problems we face cannot be solved by the same level of thinking that created
them.
Albert Einstein
Chapter 3. Extending Classes
I am, in point of fact, a particularly haughty and exclusive person, of pre-Adamite ancestral
descent. You will understand this when I tell you that I can trace my ancestry back to a
protoplasmal primordial atomic globule.
Gilbert and Sullivan, The Mikado
The quick tour (Chapter 1) described briefly how a class can be extended, or subclassed, and how an object of
an extended class can be used wherever the original class is required. The term for this capability is
polymorphism, meaning that an object of a given class can have multiple forms, either as its own class or as
any class it extends. The new class is a subclass or extended class of the class it extends; the class that is
extended is its superclass.
The collection of methods and fields that are accessible from outside a class, together with the description of
how those members are expected to behave, is often referred to as the class's contract. The contract is what the
class designer has promised that the class will do. Class extension provides two forms of inheritance:
inheritance of contract or type, whereby the subclass acquires the type of the superclass and so can be
used polymorphically wherever the superclass could be used; and
•
inheritance of implementation, whereby the subclass acquires the implementation of the superclass in
terms of its accessible fields and methods.
•
Class extension can be used for a number of purposes. It is most commonly used for specializationwhere the
extended class defines new behavior and so becomes a specialized version of its superclass. Class extension
may involve changing only the implementation of an inherited method, perhaps to make it more efficient.
Whenever you extend a class, you create a new class with an expanded contract. You do not, however, change
the part of the contract you inherit from the class you extended. Changing the way that the superclass's
contract is implemented is reasonable, but you should never change the implementation in a way that violates
that contract.
The ability to extend classes interacts with the access control mechanisms to expand the notion of contract
that a class presents. Each class can present two different contractsone for users of the class and one for