711: REFERENCE CASTING AND THE instanceof OPERATOR in Java

Create QR Code ISO/IEC18004 in Java 711: REFERENCE CASTING AND THE instanceof OPERATOR
711: REFERENCE CASTING AND THE instanceof OPERATOR
Drawing QR-Code In Java
Using Barcode creator for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
would be valid At runtime, the <reference expression> evaluates to a reference value of an object It is the type of the actual object that determines the outcome of the operation, as explained earlier What implications generics has for the instanceof operator is discussed in Section 1413, p 723 With the classes Light and String as <source type> and <destination type>, respectively, there is no subtype-supertype relationship between the <source type> and <destination type> The compiler would reject casting a reference of type Light to type String or applying the instanceof operator, as shown at (2) and (3) in Example 712 References of the classes Light and TubeLight can refer to objects of the class TubeLight (or its subclasses) in the inheritance hierarchy depicted in Figure 72 Therefore, it makes sense to apply the instanceof operator or cast a reference of the type Light to the type TubeLight as shown at (4) and (5), respectively, in Example 712 At runtime, the result of applying the instanceof operator at (4) is false, because the reference light1 of the class Light will actually denote an object of the subclass LightBulb, and this object cannot be denoted by a reference of the peer class TubeLight Applying the cast at (5) results in a ClassCastException for the same reason This is the reason why cast conversions are said to be unsafe, as they may throw a ClassCastException at runtime Note that if the result of the instanceof operator is false, the cast involving the operands will also throw a ClassCastException In Example 712, the result of applying the instanceof operator at (6) is also false, because the reference light1 will still denote an object of the class LightBulb, whose objects cannot be denoted by a reference of its subclass SpotLightBulb Thus applying the cast at (7) causes a ClassCastException to be thrown at runtime The situation shown at (8), (9), and (10) illustrates typical usage of the instanceof operator to determine what object a reference is denoting so that it can be cast for the purpose of carrying out some specific action The reference light1 of the class Light is initialized to an object of the subclass NeonLight at (8) The result of the instanceof operator at (9) is true, because the reference light1 will denote an object of the subclass NeonLight, whose objects can also be denoted by a reference of its superclass TubeLight By the same token, the cast at (10) is also valid If the result of the instanceof operator is true, the cast involving the operands will also be valid
Printing Barcode In Java
Using Barcode drawer for Java Control to generate, create barcode image in Java applications.
Example 712 The instanceof and Cast Operators class class class class class Light { /* */ } LightBulb extends Light { /* */ } SpotLightBulb extends LightBulb { /* */ } TubeLight extends Light { /* */ } NeonLight extends TubeLight { /* */ }
Barcode Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
public class WhoAmI { public static void main(String[] args) { boolean result1, result2, result3, result4, result5;
QR Code JIS X 0510 Printer In C#
Using Barcode generation for VS .NET Control to generate, create QR image in .NET framework applications.
CHAPTER 7: OBJECT-ORIENTED PROGRAMMING Light light1 = new LightBulb(); // String str = (String) light1; // result1 = light1 instanceof String; result2 = light1 instanceof TubeLight; // TubeLight tubeLight1 = (TubeLight) light1; // (1) // (2) Compile-time error // (3) Compile-time error // (4) false Peer class // (5) ClassCastException
QR Code JIS X 0510 Drawer In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create QR-Code image in ASP.NET applications.
result3 = light1 instanceof SpotLightBulb; // (6) false: Superclass // SpotLightBulb spotRef = (SpotLightBulb) light1;// (7) ClassCastException light1 = new NeonLight(); // (8) if (light1 instanceof TubeLight) { // (9) true TubeLight tubeLight2 = (TubeLight) light1; // (10) OK // Can now use tubeLight2 to access an object of the class NeonLight, // but only those members that the object inherits or overrides // from the class TubeLight } } }
Making QR Code JIS X 0510 In Visual Studio .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code image in Visual Studio .NET applications.
As we have seen, the instanceof operator effectively determines whether the reference value in the reference on the left-hand side refers to an object whose class is a subtype of the type of the reference specified on the right-hand side At runtime, it is the type of the actual object denoted by the reference on the left-hand side that is compared with the type specified on the right-hand side In other words, what matters at runtime is the type of the actual object denoted by the reference, not the declared type of the reference Example 713 provides more examples of the instanceof operator It is instructive to go through the print statements and understand the results printed out The literal null is not an instance of any reference type, as shown in the print statements (1), (2), and (16) An instance of a superclass is not an instance of its subclass, as shown in the print statement (4) An instance of a class is not an instance of a totally unrelated class, as shown in the print statement (10) An instance of a class is not an instance of an interface type that the class does not implement, as shown in the print statement (6) Any array of non-primitive type is an instance of both Object and Object[] types, as shown in the print statements (14) and (15), respectively
Painting QR-Code In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create QR-Code image in .NET framework applications.
Example 713 Using the instanceof Operator interface IStack interface ISafeStack extends IStack class StackImpl implements IStack class SafeStackImpl extends StackImpl implements ISafeStack { /* From Example 77 */ } { /* From Example 77 */ } { /* From Example 77 */ } { /* From Example 77 */ }
Drawing Bar Code In Java
Using Barcode drawer for Java Control to generate, create bar code image in Java applications.
public class Identification { public static void main(String[] args) { Object obj = new Object(); StackImpl stack = new StackImpl(10);
Creating EAN128 In Java
Using Barcode generator for Java Control to generate, create USS-128 image in Java applications.
711: REFERENCE CASTING AND THE instanceof OPERATOR SafeStackImpl safeStack = new SafeStackImpl(5); IStack iStack; Systemoutprintln("(1): " + (null instanceof Object)); Systemoutprintln("(2): " + (null instanceof IStack));
Draw Barcode In Java
Using Barcode printer for Java Control to generate, create barcode image in Java applications.
Create Code 128 Code Set A In Java
Using Barcode maker for Java Control to generate, create Code-128 image in Java applications.
Printing UPC-E Supplement 5 In Java
Using Barcode generation for Java Control to generate, create UCC - 12 image in Java applications.
Code 128C Printer In Visual Basic .NET
Using Barcode encoder for .NET Control to generate, create Code 128 Code Set B image in VS .NET applications.
UPC Code Printer In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create UPCA image in ASP.NET applications.
Bar Code Scanner In VS .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.