19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

11.9 Casting Objects and the instanceof Opera<strong>to</strong>r 425<br />

11.22 Show the output of following program:<br />

1 public class Test {<br />

2 public static void main(String[] args) {<br />

3 A a = new A(3);<br />

4 }<br />

5 }<br />

6<br />

7 class A extends B {<br />

8 public A(int t) {<br />

9 System.out.println("A's construc<strong>to</strong>r is invoked");<br />

10 }<br />

11 }<br />

12<br />

13 class B {<br />

14 public B() {<br />

15 System.out.println("B's construc<strong>to</strong>r is invoked");<br />

16 }<br />

17 }<br />

Is the no-arg construc<strong>to</strong>r of Object invoked when new A(3) is invoked?<br />

11.9 Casting Objects and the instanceof Opera<strong>to</strong>r<br />

One object reference can be typecast in<strong>to</strong> another object reference. This is called<br />

casting object.<br />

In the preceding section, the statement<br />

Key<br />

Point<br />

casting object<br />

m(new Student());<br />

assigns the object new Student() <strong>to</strong> a parameter of the Object type. This statement is<br />

equivalent <strong>to</strong><br />

Object o = new Student(); // Implicit casting<br />

m(o);<br />

The statement Object o = new Student(), known as implicit casting, is legal because an<br />

instance of Student is an instance of Object.<br />

Suppose you want <strong>to</strong> assign the object reference o <strong>to</strong> a variable of the Student type using<br />

the following statement:<br />

implicit casting<br />

Student b = o;<br />

In this case a <strong>com</strong>pile error would occur. Why does the statement Object o = new Student()<br />

work but Student b = o doesn’t? The reason is that a Student object is always<br />

an instance of Object, but an Object is not necessarily an instance of Student. Even<br />

though you can see that o is really a Student object, the <strong>com</strong>piler is not clever enough <strong>to</strong><br />

know it. To tell the <strong>com</strong>piler that o is a Student object, use explicit casting. The syntax is<br />

similar <strong>to</strong> the one used for casting among primitive data types. Enclose the target object type<br />

in parentheses and place it before the object <strong>to</strong> be cast, as follows:<br />

explicit casting<br />

Student b = (Student)o; // Explicit casting<br />

It is always possible <strong>to</strong> cast an instance of a subclass <strong>to</strong> a variable of a superclass (known as<br />

upcasting), because an instance of a subclass is always an instance of its superclass. When<br />

casting an instance of a superclass <strong>to</strong> a variable of its subclass (known as downcasting),<br />

explicit casting must be used <strong>to</strong> confirm your intention <strong>to</strong> the <strong>com</strong>piler with the<br />

upcasting<br />

downcasting

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!