13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.5 O, Templates! Where Art Thou?93this all out at runtime. That’s polymorphism—Java can deal with these differentobjects while you, the programmer, can describe them in a generalized way.One other related keyword should be menti<strong>on</strong>ed here, abstract. When<strong>on</strong>e declares a class as an abstract class, then the class itself is an incompletedefiniti<strong>on</strong>. With an abstract class you define all the data of the class but need<strong>on</strong>ly write method declarati<strong>on</strong>s, not necessarily all the code for the methods.This makes abstract classes similar to interfaces, but in an abstract class, someof the methods can be fully written out.If you’d like to know more about polymorphism, “late binding,” and moreof this aspect of Java, read Chapter 7 of Eckel’s Thinking in Java. There is anextensive example there with much more detail than we can cover here.3.5O, TEMPLATES! WHERE ART THOU?Programmers familiar with C++ may be w<strong>on</strong>dering how in the world an OOPlanguage without templates can be useful.NOTEActually, something very much like templates is available in Java 5.0. 14 A newfeature, which Sun calls generics, looks an awful lot like C++ templates(including similar syntax). It provides compile-time type checking and implicitcasting when retrieving objects from a generic c<strong>on</strong>tainer.Speaking as programmers who worked with C++ before it had templates,we can sympathize. Java’s previous lack of true templates does impose somelimits <strong>on</strong> generic programming, but not as much as <strong>on</strong>e might think. Rememberthat unlike C++, all Java classes inherit from exactly <strong>on</strong>e base class, and thatif no base class is specified, they extend the Object class. This means that everysingle Java class either directly or indirectly extends Object, and thus all Javaclasses are instances of Object. So if you need, for example, to implement ac<strong>on</strong>tainer, you can guarantee that it can c<strong>on</strong>tain any Java class by implementinga c<strong>on</strong>tainer for the Object type. Java also has runtime type identificati<strong>on</strong> featuresthat are more than a match for anything C++ has, plus it has type-safe14. Java 5.0 will <strong>on</strong>ly be out by the time this book is completed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!