I have an abstract class called account which is as follows –
abstract class Account
{
private int number;
private String owner;
public Account(int accountNumber, String accountOwner)
{
number = accountNumber;
owner = accountOwner;
}
public int getAccountNumber(){ return number; }
public String getAccountOwner(){ return owner; }
public abstract double getBalance();
public abstract double makeDeposit(double amount);
public abstract double makeWithdrawal(double amount);
}
This class defines the basic skeleton for an account. Several specialized account class will be created by inheriting this class, such as MarketSavingsAccount, RegularSavingsAccount etc.
Now I have an array which is declared as follows –
Account[] accounts
which contains a list of accounts. In this array different types of accounts are available. Now how can I determine which of these array members is an instance of MarketSavingsAccount?
From a pure OOP standpoint you’re not supposed to care; you’re supposed to avoid downcasting and treat all the Account instances the same.
But enough theory – you’re probably looking for
instanceof, as in