SECTION 4: COMMONLY ASKED LANGUAGE SPECIFIC QUESTIONS4.1) What is Downcasting?Downcasting is the term used in C++ [Stroustrup 97, p. 407] for casting a pointer or reference to a base class to a derived class. In C++, dynamic_cast is now used to perform this conversion. Downcasting is the opposite of the basic object-oriented rule, which states objects of a derived class can always be assigned to variables of a base class. Since base class variables can only sometimes be assigned to variables of a derived class (when polymorphically refering to an objects of that derived class), downcasting doesn't always work. Downcasting is therefore often both a cast and a query: Is this base variable pointing to a derived instance? In Java, the query is performed with theinstanceof operator (animal instanceof Eagle) and the conversion is
performed with the typical cast (eagle = (Eagle)animal), but in C++ this cast can return
an undefined value (Java will throw a ClassCastException if it isn't valid).
This is because Java casts are always performed at
run time while C++ casts (except for dynamic_cast) are always performed at compile time.
C++ requires polymorphic types for dynamic casts (they must contain virtual functions),
but this is not shown in the example below.
4.2) What are Virtual Functions?Look under Dynamic Binding and Polymorphism.4.3) Can I Use Multiple-Polymorphism Or Multi-Methods In C++?Yes, but you'll need to embed a dynamic typing scheme to do it. With dynamic types in place, an overriding method in a derived class can explicitly check argument types in a switch statement and invoke the desired method emulating multiple-polymorphism (See [Coplien 92].For true CLOS multi-methods, the above technique implemented as a base function (CLOS defgeneric), switching to specialized functions (CLOS methods, made friends of all arguments) will provide the functional calling syntax, multiple- polymorphism and access to parameters found in CLOS. This can require some complex switching, which is somewhat mitigated when multiple-polymorphism is implemented with virtual functions. 4.4) Can I Use Dynamic Inheritance In C++?Yes, [Coplien 92] describes a scheme where a class can contain a pointer to a base class that can switch between its derived classes, providing a limited form. Earlier chapters contain entries on bypassing C++'s message system and even bypassing static linking.Future FAQs should contain more detail.
4.101) How Can I Get Help on Java Programming?Perhaps the best source is the USENET newsgroups, also available over the Web. The most popular Java newsgroups are presently comp.lang.java.programmer and comp.lang.java.help. comp.lang.java.gui could also be helpful, among the following.
4.102) What Tutorials are Available Free Online for Java?A lot! This section will be reorganized very soon.
From Sun
Other
HUMOROUS
4.103) What Resources are Available Online for Java?Last Updated 1/9/2001
A Few Others
|