9.1 | What is the printout of running the class C in (a)? What problem arises in compiling the program in (b)? |
| |
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. |
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 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? |
9.7 | For the GeometricObject and Circle classes in Listings 9.1 and 9.2, answer the following questions:
|
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 .
|
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 } |
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)); |
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?
|
| |
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?
|
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() ? |
9.20 | Show the output of running class Test . |
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() ? |
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() ? |
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:
|
9.26 | Describe the difference between method matching and method binding. |
9.27 | What are advantages of dynamic binding? |