Java Reference
In-Depth Information
name = (String) obj; // That's better!
Telling the compiler that the type of an expression is really a different
type is known as type casting or type conversion. You perform a type
cast by prefixing the expression with the new type in parentheses. The
compiler doesn't automatically trust you when you do this and so it
checks that you are telling the truth. A smart compiler may be able to
tell at compile time that you are telling the truth; otherwise, it will insert
a run time check to verify that the cast really is allowed. If you lie to the
compiler and the run time check fails, the runtime system reports this
by throwing a ClassCastException . As the Java programming language is
strongly typed there are strict rules concerning assignments between
types.
Exercise 1.14 : Sketch out a set of classes that reflects the class struc-
ture of the Sony Walkman product family we have described. Use meth-
ods to hide the data, making all the data private and the methods pub-
lic . What methods would belong in the Walkman class? Which methods
would be added for which extended classes?
 
Search WWH ::




Custom Search