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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

94Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to Javadowncasting 15 so that in the worst case scenario, your program has a nice, cleantype excepti<strong>on</strong>. You simply do not get the kind of “mystery bugs” that you canget in C++ when you miscast an object. 16Thanks to interfaces and a true single object hierarchy, many of the usesof C++ templates go away. We doubt very much that you will miss them. Inmany cases, such as STL algorithms and other functi<strong>on</strong>al programming implementati<strong>on</strong>s,you can use interfaces to produce similar results.Critics of the Java language have a point when they complain that all thetype casting of class references in order to expose desired interfaces tends toproduce code that violates object-oriented principles. The fact that a class orinterface implements all these other named interfaces is hard-coded all over theplace in an applicati<strong>on</strong>’s code. Such critics say this is a bad thing, because it violatesencapsulati<strong>on</strong> and implementati<strong>on</strong> hiding. These critics have a point. Ifyou find yourself frequently downcasting object references, c<strong>on</strong>sider using theJava 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 (beforegenerics), all casts are runtime type safe. An excepti<strong>on</strong> is thrown if a class referenceis improperly cast. In C++, if you miscast a pointer, it assumes you meantit. Java certainly can be awkward, but errors will get caught. Sometimes that ismore important.3.6VIRTUALLY FINALOne difficulty any<strong>on</strong>e writing about Java faces is whether or not to assume yourreaders are familiar with C++. In this chapter, we have tried to help those withC++ experience without requiring such knowledge. But it is in the inevitablecomparis<strong>on</strong>s between those languages that many subtle Java features are bestdiscussed. We promised you that we would talk about the relative merits ofvirtual (a C++ c<strong>on</strong>cept) and final (a Java c<strong>on</strong>cept). To do that, we have toassume some knowledge of C++. So, let’s reverse the pattern and talk about the15. D<strong>on</strong>’t worry if this is all gibberish to you right now. We will revisit these topics in detailwhen we come up<strong>on</strong> them in the course of our sample project.16. Actually, we’re being a bit optimistic here. While Java programs are not subject to manymystery bugs, the Java Virtual Machines that run Java code are written in traditi<strong>on</strong>al languages,and there have been VMs with bugs. Time and again we see that there is no “silver bullet.” Butin our experience, Java comes close. So very close.

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

Saved successfully!

Ooh no, something went wrong!