Java Reference
In-Depth Information
ance at the level of the class (which is to say, at the level of the actual implementation of the
type) simplifies not only the language but the job of the programmers, since it makes it easy to
find the code that is being run when you call some method. If the method has been overridden,
you can find the code in the code for the class itself. If the method has not been overridden,
you can look at the most direct parent of the class and apply the same search strategy. There
are no inheritance diamonds where you need to check two possible implementation paths. But
this doesn't mean that there is no notion of multiple inheritance in Java. That notion is sup-
plied with the other aspect of the type system.
This other part of the type system is the interface , which allows the declaration of a set of
methods that must be implemented as a unit by any class that states that it implements the in-
terface. A class can implement any number of interfaces, and any class that extends another
class must implement all of the interfaces that are implemented by the class that it extends (al-
though it can also implement other interfaces). Like a class, an interface defines a type, and an
object that is an instance of a class that implements an interface is an instance of that interface
type as well. Since a class can implement multiple interfaces, the set of interfaces does not
necessarily form a tree but at best a directed graph (and, more generally, a lattice), and there
is no single root to that graph. By themselves, interfaces cannot be instantiated into objects,
but parameters and return values can be declared as interfaces. Only classes can be directly
instantiated.
Somewhere in the ontological middle between classes and interfaces are abstract classes.
These are like standard classes in that they allow one to describe an associated collection of
data, and to define a set of methods on that data, and even implement some of those methods.
But like interfaces, abstract classes also allow a method to be defined that has no implement-
ation. The goal is to allow the implementation in classes that extend the abstract class. Like
interfaces, abstract classes cannot be directly instantiated into objects. But like classes, only a
single abstract class can be extended by another class; abstract classes form part of the single-
inheritance tree of classes.
This is the core (and, during the first years of the Java language, the only) mechanism for de-
fining a type in the Java language. But that is only part of the story of the type system. The
other part is how types get used in the language.
Historically, types have been used in computer languages to allow the compiler to determine
the amount of space to allocate for objects of the associated type. This is relevant only for the
types in the Java language that are defined by classes, since only classes can have instances
that need space allocated for them. Types are also used to describe arguments and return val-
ues for methods, allowing the compiler or the runtime system to know the sizes of those en-
tities as well. If these were the only reason for types in the Java environment, there would be
Search WWH ::




Custom Search