Java Reference
In-Depth Information
14. Suppose that the following variables referring to the classes from the previous problem are declared:
Pond var1 = new Bay();
Object var2 = new Ocean();
Which of the following statements produce compiler errors? For the statements that do not produce errors, what is
the output of each statement?
((Lake) var1).method1();
((Bay) var1).method1();
((Pond) var2).method2();
((Lake) var2).method2();
((Ocean) var2).method3();
Section 9.4: Inheritance and Design
15. What is the difference between an is-a and a has-a relationship? How do you create a has-a relationship in your code?
16. Imagine a Rectangle class with objects that represent two-dimensional rectangles. The Rectangle has width and
height fields with appropriate accessors and mutators, as well as getArea and getPerimeter methods.
You would like to add a Square class into your system. Is it a good design to make Square a subclass of
Rectangle ? Why or why not?
17. Imagine that you are going to write a program to play card games. Consider a design with a Card class and 52 sub-
classes, one for each of the unique playing cards (for example, NineOfSpades and JackOfClubs ). Is this a good
design? If so, why? If not, why not, and what might be a better design?
18. In Section 9.2 we discussed adding functionality for dividend payments to the Stock class. Why was it preferable to
create a DividendStock class rather than editing the Stock class and adding this feature directly to it?
Section 9.5: Interfaces
19. What is the difference between implementing an interface and extending a class?
20. Consider the following interface and class:
public interface I {
public void m1();
public void m2();
}
public class C implements I {
// code for class C
}
What must be true about the code for class C in order for that code to compile successfully?
21. What's wrong with the code for the following interface? What should be changed to make a valid interface for
objects that have colors?
public interface Colored {
private Color color;
public Color getColor() {
return color;
}
}
Search WWH ::




Custom Search