Java Reference
In-Depth Information
14. None of the statements produces an error. This is the output of each statement:
Bay 1 Pond 2
Bay 1 Pond 2
Ocean 2
Ocean 2
Lake 3 Ocean 2
15. An is-a relationship is a subclass relationship such as those created by inheritance. A
has-a relationship occurs when one object contains a reference to another as a field.
16. Having Square extend Rectangle is a poor design because a Square cannot sub-
stitute for a Rectangle . If the client thinks the Square is a Rectangle and calls
setWidth or setHeight on it, unexpected results will occur.
17. Having each of the 52 playing cards in its own class is not a good design because
it will result in a clutter of code files without significant differences between them.
A better design would use one Card class with fields for rank and suit.
18. We made DividendStock a separate subclass from Stock for two major reasons.
First, not all stocks pay dividends, so it does not make sense for every Stock object
to have a dividends field and a payDividend method. Second, the Stock code
already worked correctly, so we did not want to tamper with it needlessly. Making
DividendStock a separate class constituted an additive and noninvasive change.
19. Extending a class causes your class to inherit all the methods and data from that
class. Implementing an interface forces you to write your own code to implement
all the methods in that interface.
20. The code for class C must contain implementations of the methods m1 and m2 to
compile correctly, because C claims to implement the I interface.
21. The interface is incorrect because interfaces can't declare fields or write bodies for
methods. The following code is a correct interface:
import java.awt.*;
// Represents items that have a color that can be retrieved.
public interface Colored {
public Color getColor();
}
22. // Represents a point with a color.
import java.awt.*;
public class ColoredPoint extends Point implements Colored {
private Color color;
Search WWH ::




Custom Search