Flylib.com

Books Software

 
 
 

Section 3.5. O, Templates Where Art Thou?


3.5. O, Templates! Where Art Thou?

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.

[14] Java 5.0 will only be out by the time this book is completed.


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. Remember 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 features that are more than a match for anything C++ has, plus it has type-safe downcasting [15] so that in the worst case scenario, your program has a nice, clean type exception. You simply do not get the kind of "mystery bugs " that you can get in C++ when you miscast an object. [16]

[15] Don't worry if this is all gibberish to you right now. We will revisit these topics in detail when we come upon them in the course of our sample project.

[16] Actually, we're being a bit optimistic here. While Java programs are not subject to many mystery bugs, the Java Virtual Machines that run Java code are written in traditional languages, and there have been VMs with bugs. Time and again we see that there is no "silver bullet." But in our experience, Java comes close. So very close.

Thanks to interfaces and a true single object hierarchy, many of the uses of C++ templates go away. We doubt very much that you will miss them. In many cases, such as STL algorithms and other functional programming implementations , you can use interfaces to produce similar results.

Critics of the Java language have a point when they complain that all the type casting of class references in order to expose desired interfaces tends to produce code that violates object-oriented principles. The fact that a class or interface implements all these other named interfaces is hard-coded all over the place in an application's code. Such critics say this is a bad thing, because it violates encapsulation and implementation hiding. These critics have a point. If you find yourself frequently downcasting object references, consider using the Java 5.0 generics, or try to find another way to code what you want to do. There may be a better way. In defense of the original Java approach (before generics), all casts are runtime type safe. An exception is thrown if a class reference is improperly cast. In C++, if you miscast a pointer, it assumes you meant it. Java certainly can be awkward , but errors will get caught. Sometimes that is more important.


3.6. Virtually Final

One difficulty anyone writing about Java faces is whether or not to assume your readers are familiar with C++. In this chapter, we have tried to help those with C++ experience without requiring such knowledge. But it is in the inevitable comparisons between those languages that many subtle Java features are best discussed. We promised you that we would talk about the relative merits of virtual (a C++ concept) and final (a Java concept). To do that, we have to assume some knowledge of C++. So, let's reverse the pattern and talk about the straight Java facts so we can let the non-C++ folks move on while we go a little deeper with you C++'ers.

In Java, a method or a class may be declared final . A method that is declared final may not be overridden in classes that extend the class containing the final implementation. A class that is declared final may not be extended at all.

Now, the comparisons to C++ require us to talk about a language feature that does not exist at all in Java. In C++, unless a method is declared virtual , when a class is used by reference to a base class (for example, when using Employee as a Person ), the base class version of the method is called. If the method is declared virtual, the version of the method called is the version for the type of Person referenced (in this case, Employee ). In Java, all methods are virtual. There is no such keyword in Java.