Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 6
The type of a reference determines how the referenced object -- that is, the object that is the value of the reference -- can be used. For instance, in the example above, code using
myComponent could invoke any of the methods defined by the class
java.awt.Component, or any of its superclasses, on the referenced object.
However, the method actually executed by a call is determined not by the type of the reference itself, but rather by the type
of the referenced object. This is the basic principle of polymorphism -- subclasses can override methods defined in the parent class in order to implement different behavior. In the case of our
example variable, if the referenced object was actually an instance of
java.awt.Button, the change in state resulting from a
setLabel("Push Me") call would be different from that resulting if the referenced object were an instance of
Besides class definitions, Java programs also use interface definitions. The difference between an interface and a class is that an interface only specifies a set of behaviors (and, in some cases, constants), while a class defines an implementation. Since interfaces do not define implementations, objects can never be instances of an interface. They can, however, be instances of classes that implement an interface. References can be of interface types, in which case the referenced objects may be instances of any class that implements the interface (either directly or through some ancestor class).
Casting is used to convert between types -- between reference types in particular, for the type of casting operation in which we're interested here. Upcast operations (also called widening conversions in the Java Language Specification) convert a subclass reference to an ancestor class reference. This casting operation is normally automatic, since it's always safe and can be implemented directly by the compiler.
Downcast operations (also called narrowing conversions in the Java Language Specification) convert an ancestor class reference to a subclass reference. This casting operation creates
execution overhead, since Java requires that the cast be checked at runtime to make sure that it's valid. If the referenced
object is not an instance of either the target type for the cast or a subclass of that type, the attempted cast is not permitted
and must throw a
instanceof operator in Java allows you to determine whether or not a specific casting operation is permitted without actually attempting
the operation. Since the performance cost of a check is much less than that of the exception generated by an unpermitted cast
attempt, it's generally wise to use an
instanceof test anytime you're not sure that the type of a reference is what you'd like it to be. Before doing so, however, you should
make sure that you have a reasonable way of dealing with a reference of an unwanted type -- otherwise, you may as well just
let the exception be thrown and handle it at a higher level in your code.