|
3.6. Virtually FinalOne 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. |
|