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.8 Dynamic Binding 423<br />

C n C n-1 . . . . .<br />

C 2 C 1<br />

java.lang.Object<br />

If o is an instance of C 1 , o is also an<br />

instance of C 2 , C 3 , …, C n-1 , and C n<br />

FIGURE 11.2<br />

The method <strong>to</strong> be invoked is dynamically bound at runtime.<br />

Listing 11.6 gives an example <strong>to</strong> demonstrate dynamic binding.<br />

LISTING 11.6<br />

DynamicBindingDemo.java<br />

1 public class DynamicBindingDemo {<br />

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

3<br />

4<br />

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

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

5 m(new Person());<br />

6 m(new Object());<br />

7 }<br />

8<br />

9 public static void m(Object x) {<br />

10 System.out.println(x.<strong>to</strong>String());<br />

11 }<br />

12 }<br />

13<br />

14 class GraduateStudent extends Student {<br />

15 }<br />

16<br />

17 class Student extends Person {<br />

18 @Override<br />

19 public String <strong>to</strong>String() {<br />

20 return "Student";<br />

21 }<br />

22 }<br />

23<br />

24 class Person extends Object {<br />

25 @Override<br />

26 public String <strong>to</strong>String() {<br />

27 return "Person";<br />

28 }<br />

29 }<br />

VideoNote<br />

Polymorphism and dynamic<br />

binding demo<br />

polymorphic call<br />

dynamic binding<br />

override <strong>to</strong>String()<br />

override <strong>to</strong>String()<br />

Student<br />

Student<br />

Person<br />

java.lang.Object@130c19b<br />

Method m (line 9) takes a parameter of the Object type. You can invoke m with any object<br />

(e.g., new GraduateStudent(), new Student(), new Person(), and new Object())<br />

in lines 3–6).<br />

When the method m(Object x) is executed, the argument x’s <strong>to</strong>String method is<br />

invoked. x may be an instance of GraduateStudent, Student, Person, or Object. The<br />

classes GraduateStudent, Student, Person, and Object have their own implementations<br />

of the <strong>to</strong>String method. Which implementation is used will be determined by x’s<br />

actual type at runtime. Invoking m(new GraduateStudent()) (line 3) causes the<br />

<strong>to</strong>String method defined in the Student class <strong>to</strong> be invoked.<br />

Invoking m(new Student()) (line 4) causes the <strong>to</strong>String method defined in the<br />

Student class <strong>to</strong> be invoked; invoking m(new Person()) (line 5) causes the <strong>to</strong>String

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

Saved successfully!

Ooh no, something went wrong!