THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

In this example, oref is correctly assigned references to Pixel and String objects even though those
classes have no relationship except that both have Object as a superclass. The Object class also defines
several important methods that you'll learn about in Chapter 3.


1.11.3. Type Casting


The following code fragment seems quite reasonable (if not particularly useful) but results in a compile-time
error:


String name = "Petronius";
Object obj = name;
name = obj; // INVALID: won't compile


We declare and initialize a String reference which we then assign to a general-purpose Object reference,
and then we try to assign the reference to a String back to the String reference. Why doesn't this work?
The problem is that while a String is always an Object, an Object is not necessarily a String, and
even though you can see that in this case it really is a String, the compiler is not so clever. To help the
compiler you have to tell it that the object referred to by obj is actually a String and so can be assigned to
name:


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 structure of the Sony Walkman product family
we have described. Use methods to hide the data, making all the data private and the methods public.
What methods would belong in the Walkman class? Which methods would be added for which extended
classes?


1.12. Interfaces


Sometimes you want only to declare methods an object must support but not to supply the implementation of
those methods. As long as their behavior meets specific criteriacalled the contractimplementation details of
the methods are irrelevant. These declarations define a type, and any class that implements those methods can
be said to have that type, regardless of how the methods are implemented. For example, to ask whether a
particular value is contained in a set of values, details of how those values are stored are irrelevant. You want
the methods to work equally well with a linked list of values, a hashtable of values, or any other data
structure.


To support this, you can define an interface. An interface is like a class but has only empty declarations of its
methods. The designer of the interface declares the methods that must be supported by classes that implement

Free download pdf