[ Team LiB ] 6.7 Polymorphism and Dynamic
Wednesday, November 22nd, 2006[ Team LiB ] Page 342
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
[ Team LiB ] Page 342
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
[ Team LiB ] 6.7 Polymorphism and Dynamic Method Lookup Which object a reference will actually denote during runtime, cannot always be determined at compile time. Polymorphism allows a reference to denote objects of different types at different times during execution. A supertype reference exhibits polymorphic behavior, since it can denote objects of its subtypes. When a non-private instance method is invoked on an object, the method definition actually executed is determined both by the type of the object at runtime and the method signature. Dynamic method lookup is the process of determining which method definition a method signature denotes during runtime, based on the type of the object. However, a call to a private instance method is not polymorphic. Such a call can only occur within the class, and gets bound to the private method implementation at compile time. The inheritance hierarchy depicted in Figure 6.5 is implemented in Example 6.14. The implementation of the method draw() is overridden in all subclasses of the class Shape. The invocation of the draw() method in the two loops at (3) and (4) in Example 6.14, relies on the polymorphic behavior of references and dynamic method lookup. The array shapes holds Shape references denoting a Circle, a Rectangle and a Square, as shown at (1). At runtime, dynamic lookup determines the draw() implementation to execute, based on the type of the object denoted by each element in the array. This is also the case for the elements of the array drawables at (2), which holds IDrawable references that can be assigned any object of a class that implements the IDrawable interface. The first loop will still work without any change if objects of new subclasses of the class Shape are added to the array shapes. If they did not override the draw() method, then an inherited version of the method would be executed. This polymorphic behavior applies to the array drawables, where the subtype objects are guaranteed to have implemented the IDrawable interface. Figure 6.5. Polymorphic Methods Page 340
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
Page 341
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services
[ Team LiB ] Page 339
Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Virtualwebstudio java web hosting provider
Page 337
Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Virtualwebstudio java web hosting provider
Page 338
Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Virtualwebstudio java web hosting provider
Page 336
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services
[ Team LiB ] Review Questions 6.17 Given the following program, which statement is true? // Filename: MyClass.javapublic class MyClass { public static void main(String[] args) { A[] arrA; B[] arrB; arrA = new A[10]; arrB = new B[20]; arrA = arrB; // (1) arrB = (B[]) arrA; // (2) arrA = new A[10]; arrB = (B[]) arrA; // (3) } } class A {} class B extends A {} Select the one correct answer. a. b. c. d. The program will fail to compile because of the assignment at (1). The program will throw a java.lang.ClassCastException in the assignment at (2) when run. The program will throw a java.lang.ClassCastException in the assignment at (3) when run. The program will compile and run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed. e. The program will compile and run without errors, but will not do so if the (B[]) cast in statements at (2) and (3) is removed. 6.18 Which is the first line that will cause compilation to fail in the following program? // Filename: MyClass.javaclass MyClass { public static void main(String[] args) { MyClass a; MySubclass b; a = new MyClass(); // (1) b = new MySubclass(); // (2) a = b; // (3) Page 335
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services
[ Team LiB ] Page 334
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services
Page 333
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider