Of the five statements, only statement C avoids causing a ClassCastException. This is because of the casting present in each statement. The program first creates three objects: the BankAccount object b, the SavingsAccount object s, and the CheckingAccount object c. Statement A attempts to cast b into a SavingsAccount object, but they are not compatible, so statement A is false. Statement B attempts to cast b into a CheckingAccount object, but they are also not compatible, so statement B is false. Statement C attempts to recast c into a CheckingAccount object, then tells it to perform the deposit method. This is entirely valid, so statement C is true. Statement D attempts to cast s into a CheckingAccount object, but they are not compatible, so statement D is false. Finally, statement E attempts to cast c into a SavingsAccount object, but, again, they are not compatible, so statement E is false.
−Phoenix
Spot on, and you are 800 XPs wealthier for being so.
ReplyDelete