Java Reference
In-Depth Information
this all out at runtime. That's polymorphism—Java can deal with these different
objects while you, the programmer, can describe them in a generalized way.
One other related keyword should be mentioned here, abstract . When
one declares a class as an abstract class, then the class itself is an incomplete
definition. With an abstract class you define all the data of the class but need
only write method declarations, not necessarily all the code for the methods.
This makes abstract classes similar to interfaces, but in an abstract class, some
of the methods can be fully written out.
If you'd like to know more about polymorphism, “late binding,” and more
of this aspect of Java, read Chapter 7 of Eckel's Thinking in Java . There is an
extensive example there with much more detail than we can cover here.
3.5
O, T EMPLATES ! W HERE A RT T HOU ?
Programmers familiar with C++ may be wondering how in the world an OOP
language without templates can be useful.
NOTE
Actually, something very much like templates is available in Java 5.0. 14 A new
feature, which Sun calls generics , looks an awful lot like C++ templates
(including similar syntax). It provides compile-time type checking and implicit
casting when retrieving objects from a generic container.
Speaking as programmers who worked with C++ before it had templates,
we can sympathize. Java's previous lack of true templates does impose some
limits on generic programming, but not as much as one might think. Remem-
ber that unlike C++, all Java classes inherit from exactly one base class, and that
if no base class is specified, they extend the Object class. This means that every
single Java class either directly or indirectly extends Object , and thus all Java
classes are instances of Object . So if you need, for example, to implement a
container, you can guarantee that it can contain any Java class by implementing
a container for the Object type. Java also has runtime type identification fea-
tures that are more than a match for anything C++ has, plus it has type-safe
14. Java 5.0 will only be out by the time this topic is completed.
Search WWH ::




Custom Search