Classifying cast outcomes in a bank hierarchy

A Java banking program defines these classes:


Java
class Account { }
class Savings extends Account { }
class Checking extends Account { }
class Premium extends Savings { }


It then declares Account a = new Premium(); and Savings s = new Savings();. Treat each statement in the table on its own, as if it were the only statement after these two declarations. In the Outcome column, type OK if the statement compiles and runs, COMPILE if the compiler rejects it, or RUNTIME if it compiles but throws a ClassCastException.

StatementOutcome
Savings x = (Savings) a;
Checking c = (Checking) s;
Premium p = (Premium) s;
Premium q = a;
Account b = s;
Checking d = (Checking) a;

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Type Conversion and Casting Questions: