COP-2800 Homework #5 Model Solution 11.1) What is the printout of running the class C in (a)? a) class A { public A() { System.out.println( "A's no-arg constructor is invoked" ); } } class B extends A { } public class C { public static void main(String[] args) { B b = new B(); } } -- Output: "A's no-arg constructor is invoked" What problem arises in compiling the program in (b)? b) class A { public A(int x) { } } class B extends A { public B() { } } public class C { public static void main(String[] args) { B b = new B(); } } -- Cannot compile since the no-arg constructor of B attempts -- to invoke the no-arg of constructor of A, which doesn't exist. 11.2) True or false? 1. 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.) 2. 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) 3. You can override a private method defined in a superclass. -- False (you can only override *accessible* instance methods.) 4. You can override a static method defined in a superclass. -- False (you can only override accessible *instance* methods.) 11.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. } -- Line 5 should be changed to: -- this.radius = radius; -- -- Line 21 and line 22 should be changed to: -- super(radius); -- this.length = length; -- -- Line 27 should be changed to: -- return super.getArea() * length; 11.11) Show the output of the following program: public class Test { public static void main(String[] args) { A a = new A(3); } } class A extends B { public A(int t) { System.out.println("A's constructor is invoked"); } } class B { public B() { System.out.println("B's constructor is invoked"); } } -- The output is: -- B's constructor is invoked -- A's constructor is invoked 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. 11.13) Suppose that Fruit, Apple, Orange, Golden Delicious Apple, and Macintosh Apple are declared, as shown in Figure 11.6: Fruit -----------^----------- Apple Orange -------^---------- GoldenDelicious Macintosh (Figure 11.6: GoldenDelicious and Macintosh are subclasses of Apple; Apple and Orange are subclasses of Fruit.) Assume that the following declaration[s] is [are] given: Fruit fruit = new GoldenDelicious(); Orange orange = new Orange; Answer the following questions: 1) Is fruit instanceof Fruit? -- true 2) Is fruit instanceof Orange? -- false 3) Is fruit instanceof Apple? -- true 4) Is fruit instanceof GoldenDelicious? -- true 5) Is fruit instanceof Macintosh? -- false 6) Is orange instanceof Orange? -- true 7) Is orange instanceof Fruit? -- true 8) Is orange instanceof Apple? -- false 9) Suppose the method makeApple Cider is defined in the Apple class. Can fruit invoke this method? -- Yes Can orange invoke this method? -- No 10) Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? -- Yes Can fruit invoke this method? -- No 11) Is the statement Orange p = new Apple() legal? -- No 12) Is the statement Macintosh p = new Apple() legal? -- No 13) Is the statement Apple p = new Macintosh() legal? -- Yes 11.15) 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 code below; 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), respectively. public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } a) class Circle { double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } -- 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) class Circle { double radius; public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } } -- 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. 11.21) In the code below, classes A and B are in different packages. a) package p1; public class A { ?????? int i; ?????? void m() { ... } } b) package p2; public class B extends A { public void m1(String[] args) { System.out.println(i); m(); } } If the question marks are replaced by blanks, can class B be compiled? -- No (Can only be accessed from within the same package) If the question marks are replaced by private, can class B be compiled? -- No (Can only be accessed from within the same class) If the question marks are replaced by protected, can class B be compiled? -- Yes (Can be accessed from within the same package, *or by subclasses*)