Converting Between Class and Interface Types

BankAccount account = new BankAccount(10000);
 Measurable x = account; // OK

Coin dime = new Coin(0.1, "dime");
 Measurable x = dime; // Also OK

Because Rectangle doesn't implement Measurable

Casts

DataSet coinData = new DataSet();
coinData.add(new Coin(0.25, "quarter"));
coinData.add(new Coin(0.1, "dime"));
. . .
Measurable max = coinData.getMaximum(); // Get the largest coin

String name = max.getName(); // ERROR

Coin maxCoin = (Coin) max;
String name = maxCoin.getName();


Can you use a cast (BankAccount) x to convert a Measurable variable x to a BankAccount reference?

   Answer: Only if x actually refers to a BankAccount object.


If both BankAccount and Coin implement the Measurable interface, can a Coin reference be converted to a BankAccount reference?

   Answer: No – a Coin reference can be converted to a Measurable reference,
                  but if you attempt to cast that reference to a
BankAccount, an exception occurs.