previous |
start |
next
Casts
- Add coin objects to DataSet
DataSet coinData = new DataSet();
coinData.add(new Coin(0.25, "quarter"));
coinData.add(new Coin(0.1, "dime"));
...
- Get largest coin with getMaximum method:
Measurable max = coinData.getMaximum();
- What can you do with it? It's not of type Coin
String name = max.getName(); // ERROR
- You know it's a coin, but the compiler doesn't. Apply a
cast:
Coin maxCoin = (Coin)max;
String name = maxCoin.getName();
- If you are wrong and max isn't a coin, the compiler
throws an exception
previous |
start |
next