10.9. Chapter Summary

 
[Page 332 ( continued )]

Review Questions

Sections 9.2 “9.5

9.1 What is the printout of running the class C in (a)? What problem arises in compiling the program in (b)?

[Page 333]
9.2 True or false? (1) A subclass is a subset of a superclass. (2) When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked. (3) You can override a private method defined in a superclass. (4) You can override a static method defined in a superclass.
9.3 Identify the problems in the following classes:
 1   public class   Circle { 2   private double   radius; 3 4   public   Circle (   double   radius) { 5 radius = radius; 6 } 7 8   public double   getRadius() { 9   return   radius; 10 } 11 12   public double   getArea() { 13   return   radius * radius * Math.PI; 14 } 15 } 16 17   class   B   extends   Circle { 18   private   double length; 19 20 B(   double   radius,   double   length) { 21 Circle(radius); 22 length = length; 23 } 24 25  /** Override getArea() */  26   public double   getArea() { 27   return   getArea() * length; 28 } 29 } 

9.4 Explain the difference between method overloading and method overriding.

Section 9.6 The Object class and Its toString() Method

9.5 Does every class have a toString method? Where does it come from? How is it used? Is it appropriate to override this method?
9.6 Show the output of following program:
 1   public class   Test { 2   public static void   main(String[] args) { 3 A a =   new   A(3); 4 } 5 } 6 7   class   A   extends   B { 8   public   A(   int   t) { 9 System.out.println(   "A's constructor is invoked"   ); 10 } 11 } 12 

[Page 334]
 13   class   B { 14   public   B() { 15 System.out.println(   "B's constructor is invoked"   ); 16 } 17 } 

Is the no-arg constructor of Object invoked when new A(3) is invoked?

Sections 9.6 “9.7

9.7 For the GeometricObject and Circle classes in Listings 9.1 and 9.2, answer the following questions:
  1. Assume that circle is a Circle object and object1 is a Geometric-Object , are the following Boolean expressions true or false?

     (circle   instanceof   GeometricObject) (object1   instanceof   GeometricObject) (circle   instanceof   Circle) (object1   instanceof   Circle) 
  2. Are the following statements correct?

     Circle circle =   new   Circle(   5   ); GeometricObject object = circle; 
  3. Are the following statements correct?

     GeometricObject object =   new   GeometricObject(); Circle circle = (Circle)object; 
9.8 Suppose that Fruit , Apple , Orange , Golden Delicious Apple , and Macintosh Apple are declared, as shown in Figure 9.11. Assume that fruit is an instance of GoldenDelicious and orange is an instance of Orange . Answer the following questions:
Figure 9.11. GoldenDelicious and Macintosh are subclasses of Apple , Apple and Orange are subclasses of Fruit .


  1. Is fruit instanceof Orange ?

  2. Is fruit instanceof Apple ?

  3. Is fruit instanceof GoldenDelicious ?

  4. Is fruit instanceof Macintosh ?

  5. Is orange instanceof Orange ?

  6. Is orange instanceof Fruit ?

  7. Is orange instanceof Apple ?


    [Page 335]
  8. Suppose the method makeApple is defined in the Apple class. Can fruit invoke this method? Can orange invoke this method?

  9. Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? Can fruit invoke this method?

9.9 What is wrong in the following code?
 1   public class   Test { 2   public static void   main(String[] args) { 3 Object fruit =   new   Fruit(); 4 Object apple = (Apple)fruit; 5 } 6 } 7 8   class   Apple   extends   Fruit { 9 } 10 11   class   Fruit { 12 } 

Section 9.9 The ArrayList Class

9.10 How do you create an ArrayList ? How do you append an object to a list? How do you insert an object at the beginning of a list? How do you find the number of objects in a list? How do you remove a given object from a list? How do you remove the last object from the list? How do you check whether a given object is in a list? How do you retrieve an object at a specified index from a list?
9.11 Identify three errors in the following code:
 ArrayList list =   new   ArrayList(); list.add(   "New York"   ); list.add(   "Austin"   ); list.add(   new   java.util.Date()); String city = list.get(     ); list.set(3,   "Dallas"   ); System.out.println(list.get(3)); 

Section 9.11 The protected Data and Methods

9.12 What modifier should you use on a class so that a class in the same package can access it, but a class in a different package cannot access it?
9.13 What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it?
9.14 In the following code, classes A and B are in the same package. If the question marks are replaced by blanks, can class B be compiled? If the question marks are replaced by private , can class B be compiled? If the question marks are replaced by protected , can class B be compiled?


[Page 336]
9.15 In the following code, classes A and B are in different packages. If the question marks are replaced by blanks, can class B be compiled? If the question marks are replaced by private , can class B be compiled? If the question marks are replaced by protected , can class B be compiled?

Section 9.13 The Methods in the Object Class

9.16 When overriding the equals method, a common mistake is mistyping its signature in the subclass. For example, the equals method is incorrectly written as equals(Circle circle) , as shown in (a) in the following code, instead, it should be equals(Object circle) , as shown in (b). Show the output of running class Test with the Circle class in (a) and in (b).
9.17 Where are the equals , hashCode , finalize , clone , and getClass methods defined? When is the finalize method invoked?
9.18 Since the clone method is defined in Object , every object can invoke this method. Is it true?
9.19 What is the return type of the getClass method? If two objects o1 and o2 have the same class type, is o1.getClass() same as o2.getClass() ?

Section 9.14 Hiding Fields and Static Methods

9.20 Show the output of running class Test .
[Page 337]
9.21 For the HidingDemo class in Listing 9.10, what is the output if line 3 is replaced by B x = new B() ? What error would occur if line 3 is replaced by A x = new A() ?

Section 9.15 Initialization Blocks

9.22 Show the output of following program:
9.23 For the InitializationDemo class in Listing 9.11, what is the output if line 3 is deleted? What is the output if line 3 is replaced by new N() ? What is the output if line 3 is replaced by N object = new M() ?

[Page 338]

Comprehensive

9.24 Define the following terms: inheritance, superclass, subclass, the keywords super and this , casting objects, the modifiers protected and final .
9.25 Indicate true or false for the following statements:
  • A protected datum or method can be accessed by any class in the same package.

  • A protected datum or method can be accessed by any class in different packages.

  • A protected datum or method can be accessed by its subclasses in any package.

  • A final class can have instances.

  • A final class can be extended.

  • A final method can be overridden.

  • You can always successfully cast an instance of a subclass to a superclass.

  • You can always successfully cast an instance of a superclass to a subclass.

  • The order in which modifiers appear before a method is important.

9.26 Describe the difference between method matching and method binding.
9.27 What are advantages of dynamic binding?
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net