Java Reference
In-Depth Information
8 }
9
10 // returns this cash investment's market value, which
11 // is equal to the amount of cash
12 public double getMarketValue() {
13 return amount;
14 }
15
16 // since cash is a fixed asset, it never has any profit
17 public double getProfit() {
18 return 0.0;
19 }
20
21 // sets the amount of cash invested to the given value
22 public void setAmount( double amount) {
23 this .amount = amount;
24 }
25 }
As we discussed earlier in this chapter, a DividendStock is very similar to a
normal Stock , but it has a small amount of behavior added. Let's display
DividendStock as a subclass of Stock through inheritance, matching the design
from earlier in the chapter. Figure 9.8 shows how our hierarchy should now look.
<<interface>>
Asset
getMarketValue()
getProfit()
Stock
symbol
total shares: int
total cost
current price
getMarketValue()
getProfit()
MutualFund
symbol
total shares: double
total cost
current price
getMarketValue()
getProfit()
Cash
amount
getMarketValue()
getProfit()
DividendStock
dividends
getMarketValue()
getProfit()
Figure 9.8
Financial class hierarchy
Search WWH ::




Custom Search