Originally posted by Bert Bates:
Could you be a bit more specific about what we didn't cover - we'd like to make sure it's in the next edition!
Bert,
Without giving away actual test questions I've tried to recreate the subject matter in a few examples below. The topic that I was referring to is Class casting or Object casting (using a class cast on an existing object reference or when instantiating a new object).
There were two questions I had to answer that used class casting. One of them I clearly remember, the other I remember only that it used syntax and structure that was quite similar to something I had seen in a mock exam (I was in a bit of a mental fog that day or I would have better recall
. I spent a lot of time reading your book, and even wrote up an extensive study guide for myself to make sure I hadn't skipped any topics. Perhaps I overlooked something, but this topic does not seem to be covered. I also reviewed the exam objectives, and the way that this subject matter was presented doesn't fall neatly into just one objective (it fits most clearly into section 6 and Object Orientation obviously, but also section 5 and applying assignment operators to any class scope may apply as well).
I'll start with the second, less memorable, type topic. Whizlabs has a series of questions that ask specifically about using class cast statements on a superclass and two direct sublasses that are analogous to the following example:
class Dad{}
class Brother extends Dad{}
class Sister extends Dad{}
class WhoIsWho{
public static void main(
String[] args){
Dad d = new Dad();
Brother bro = new Brother();
Sister sis = new Sister();
/* Each of 3 mock exam questions would contain this type of class hierarchy and object instantiation, with one of the lines below, and a question asking you to analyze four statements about the code (i.e. compiles and runs, compiles and throws exception, etc.)
*/
// example 1. compiles and runs without Exception
Dad d2 = (Dad) bro;
// example 2. Will compile, throws ClassCastException at runtime
Sister sis2 = (Sister) d;
// example 3. Will not compile
Brother bro2 = (Brother) sis;
}
}
Discussion:
Example 1 above is fairly obvious and clearly follows OO rules that are covered by Chapter 5, however, since class casting does not seem to be directly addressed it may leave some doubt in reader's minds. Example 3 above also seems fairly obvious if one considers basic OO rules. Example 2 is where the real confusion may start. Chapter 5 has an excellent discussion of the issues surrounding Reference variable type vs. object type in cases where you have a compound reference/object type assignment scenario and such an object is used in overloaded and overridden methods (and the topic of dynamic method invocation regarding this subject is also given some attention). There are also many cases where the book specifically addresses and stresses the issue of primitive type casting. What seemed to be lacking was a clear (if however brief) addressing of the issue of class casts. While I could memorize the scenario above with regard to class casting of superclasses, subclasses, and siblings, I'm not sure I fully understand the principles that underlie the question of why the compiler/JVM treats class casting in this way.
The second example deals with the same topic, although in retrospect I believe that the question wasn't specifically asking about this subject matter. However, my uncertainty about some aspects of class casting (also referred to as conversion in some documents I've seen) led me to wonder about whether the code would compile vs. throw a RuntimeException. This example recreates the subject matter without revealing the actual question. It also contains a few tests that I performed to clarify what was going on. The code in the first section represents what code needs to be analyzed to answer the question about what the output would be, and the choice of compile error vs. RuntimeException must be made:
class ObjectCasting2{
public static void main(String[] args) {
// Example: illustrates the nature of the topic
String twine = new String("twine");
Object rope = (Object) twine;
System.out.println( twine.equals(rope) + " " + rope.equals(twine) );
// testing class hierarchy of Example:
System.out.println( (rope instanceof Object) + " " + (twine instanceof Object) );
System.out.println( (rope instanceof String) + " " + (twine instanceof String) );
/* More testing: direct reference variable assignment of rope2 has more or less the same effect as the cast in assignment of rope above
*/
Object rope2 = new String("twine");
System.out.println(twine.equals(rope2)+ " " + rope2.equals(twine));
}
}
Discussion:
There are a few topics wrapped up here: overriding, particularly as it applies to the equals method in Object vs. String; class hierarchy and
polymorphism; and aspects of the nature of String objects. All of these are topics I was fairly comfortable with. What threw me was the class cast. The reason it threw me is that even though I've used class casts, for the reasons discussed in the first example above, I now found myself wondering if there wasn't a hidden complexity lurking here related to the cast itself. In both the examples above, taking a few moments to write and test some code helped clarify the issue, but in the exam, this kind of nagging little question takes on ridiculous proportions that outweigh it's overall significance. (I think I actual answered the second question correctly anyway).
I hope this is helpful. Again, just to be clear, the book is a great resource. I started preparing for the cert exam last summer, but then stopped when I realized that a new exam was being introduced. I started preparing again in late April. Prior to this book, I had looked at a number of preparation materials and this really was the first one that inspired real confidence (not in using the Java language itself, but confidence in knowing what would be expected in the exam).
Thanks again,
Mark