Hints for Lab 5
To check if a variable points to an object of a certain class you can use the operator
instanceof
. For example, to check if newAcc
points to an
object of the class CheckingAccount
you could use
if (newAcc instanceof CheckingAccount)
.
Remember that if a variable of a certain class C
is pointing to an object of
a subclass of C
, the compiler will issue compilation errors if a method of
the subclass is invoked. For example, if newAcc
is of type
BankAccount
and it points to an object of the class CheckingAccount
,
then the compiler will print an error message with this statement newAcc.getTransactionCount()
because method getTransactionCount()
has not been defined in class
BankAccount
. To fix the error you must use casting: Cast the variable
newAcc
to type CheckingAccount
(((CheckingAccount)newAcc).getTransactionCount()
).