In the first object assignment, the compiler implicitly casts a SavingsAccount type to an
Account type. This is done because all instances of SavingsAceount are also instances of Account
(upcasting). The second object assignment fails at compilation since we are attempting to
implicitly downcast an Account type to a SavingsAccount. Not all instances of Account are also
instances of SavingsAeeount even though in our example we know that fredsAecount contains
an instance of SavingsAccount. We must explicitly downcast, so the following statement would
be valid:
fredsSavingAccount = (SavingsAccount) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.