COP-2800 Homework #5 Model Solution Answer the following questions as briefly (but completely) as possible: 1) What is the printout of running the class C (via the command “java C”) in the code below (saved in C.java)? (And, is that what you expected?) 1: class A { 2: public A () { 3: System.out.println( "A's no-arg constructor is invoked" ); 4: } 5: } 6: 7: class B extends A { 8: } 9: 10: public class C { 11: public static void main ( String [] args ) { 12: B b = new B(); 13: } 14: } -- Output: "A's no-arg constructor is invoked" 2) What problem do you expect to see if compiling the program below? What was the error(s), if any, actually produced? 1: class A { 2: public A ( int x ) { 3: } 4: } 5: 6: class B extends A { 7: public B () { 8: } 9: } 10: 11: public class C { 12: public static void main ( String [] args ) { 13: B b = new B(); 14: } 15: } -- Cannot compile since the no-arg constructor of B attempts -- to invoke the no-arg of constructor of A, which doesn't exist. 3) Which of the follow statements are true? Which are false? Explain why. a. A subclass is a subset of a superclass. -- False (this depends on your point of view: all subclass objects -- are also superclass objects, that is, all Llamas are Animals; but -- usually the subclass, or child or derived class, contains all the -- fields and methods of the superclass, plus more, so subclass' -- object's members are a superset of the superclass' object's members.) b. When invoking a constructor from a subcass, its superclass's no-arg constructor is always invoked. -- False (only if no other constructor is explicitly invoked) c. You can override a private method defined in a superclass. -- False (you can only override *accessible* instance methods.) d. You can override a static method defined in a superclass. -- False (you can only override accessible *instance* methods.) 4) What is the benefit of using the @Override annotation? -- zzz 5) a. Show the output of the 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: } -- The output is: -- B's constructor is invoked -- A's constructor is invoked b.Is the no-arg constructor of Object invoked when new A(3) is invoked? -- Yes, class Object's constructor is invoked before any statements -- in class B's constructor are executed. 6) Indicate true or false for the follow statements: a. You can always successfully cast an instance of a subclass to a superclass. -- True (since a subclass is-a superclass) b. You can always successfully cast an instance of a superclass to a subclass. -- False (This only works if the object really is an instance of the subclass) 7) What's wrong with 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: } -- On line 4, you are casting a non-Applet object to an Apple. That results -- in a runtime error: -- Exception in thread "main" java.lang.ClassCastException: Fruit cannot -- be cast to Apple -- at Test.main(num7.java:4) 8) 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) below. It should be written as equals(Object circle), as shown in (b) below. Show the output of running class Test using the Circle class first from (a), and then from (b). Explain the output. 1: public class Test { 2: public static void main ( String [] args ) { 3: Object circle1 = new Circle(); 4: Object circle2 = new Circle(); 5: System.out.println( circle1.equals( circle2 ) ); 6: } 7: } a) 1: class Circle { 2: double radius; 3: public boolean equals ( Circle circle ) { 4: return this.radius == circle.radius; 5: } 6: } -- The output is "false" in this case. The Circle class has two -- overloaded methods: equals(Circle circle) defined in the Circle -- class and equals(Object circle) defined in the Object class, -- inherited by the Circle class. At compilation time, -- circle1.equals(circle2) is matched to equals(Circle circle), -- because equals(Circle circle) is more specific than -- equals(Object circle). b) 1: class Circle { 2: double radius; 3: public boolean equals ( Object circle ) { 4: return this.radius == ((Circle) circle).radius; 5: } 6: } -- The output is "true" this time. The Circle class overrides the -- equals(Object circle) method defined in the Object class. At -- compilation time, circle1.equals(circle2) is matched to -- equals(Circle circle) and at runtime, the equals(Object circle) -- method implemented in the Circle class is invoked. Next, try adding the “@Override” annotation to the equals method in (a), and then try compiling. Repeat with (b). What are the results, and are they as you expected? -- ??? 9) How would you prevent a class from being extended? How would you prevent a method from being overridden? -- ??? 10) Which of the following classes define legal abstract classes? a. 1: class A { 2: abstract void unfinished ( ) { 3: } 4: } -- ??? b. 1: public class abstract A { 2: abstract void unfinished ( ) { 3: } 4: } -- ??? c. 1: class A { 2: abstract void unfinished ( ) ; 3: } -- ??? d. 1: abstract class A { 2: protected void unfinished ( ) ; 3: } -- ??? e. 1: abstract class A { 2: abstract void unfinished ( ) ; 3: } -- ??? f. 1: class A { 2: abstract int unfinished ( ) ; 3: } -- ??? 11) Suppose A is an interface. Can you create an instance using “new A()”? -- ??? 12) Which of the following (if any) is a correct interface declaration? (Assume I1 and I2 are correctly defined elsewhere.) a. 1: interface A { 2: void print () { 3: }; 4: } -- ??? b. 1: abstract interface A extends I1, I2 { 2: abstract void print () { 3: }; 4: } -- ??? c. 1: abstract interface A { 2: print () ; 3: } -- ??? d. 1: interface A { 2: void print () ; 3: } -- ???