user3526935
user3526935

Reputation: 43

Better way then If(x instanceof y) in Java?

I have those lines of code:

    public String getAccountType(BankAccount a){
       if(a instanceof RegularAccount)
           return "RA";
       else if(a instanceof SavingsAccount)
           return "SA";
       else if(a instanceof cityAccount)
           return "CLA";
       else if(a instanceof StateLawAccount)
           return "SLA";
       else if(a instanceof FederationLawAccount)
           return "FLA";
       else
           return null;
    }

BankAccount is the super class (abstract) of all classes below. In this method, I just want to return what "a" class is within a String.

But, I was wondering if there is a better way to verify "a" class other than this bunch of if/else statements. Is there one? Could I do it with switch statement? If so, how?

Upvotes: 4

Views: 1120

Answers (3)

Aragorn
Aragorn

Reputation: 5289

Put a string class member in the abstract class and override its getter method in the implementation to return specific type:

public abstract class BankAccount{
    String accountType;

    public abstract String getAccountType(){
        return accountType;
    }
}

Upvotes: 1

Laf
Laf

Reputation: 8215

In your case, I'd add an abstract method getAccountType () to the BackAccount class, and implement it in each concrete class so that they return the correct account type.

public abstract class BankAccount {
    public abstract String getAccountType ();

    // Rest of implementation
}

public class RegularAccount extends BankAccount {
    private static final String ACCOUNT_TYPE = "RA";

    @Override
    public String getACcountType () {
        return ACCOUNT_TYPE;
    }

    // Rest of implementation
}

There is no easier way to compare an object's class than the code example you have provided with your question. However, if you end up having to do this in your code, then you should review your current design.

Consider the case where your manager asks you to add two additional back account types (AccountX and AccountY), and asks you to remove the StateLawAccount (for some reasons). Not only do you have to add the new classes in your code for account types AccountX and AccountY and remove the existing StateLawAccount, but you also have to remember that this unrelated class has this huge if/else that checks for bank account types. If you forget about it, then you have inserted a new bug in your software.

Upvotes: 2

user41871
user41871

Reputation:

Put an abstract method getAccountType() on BankAccount, and then have the implementations return the account type string. Here's an example that assumes that BankAccount is an interface:

public interface BankAccount {

    String getAccountType();

    ... whatever else ...
}

Then

public class RegularAccount implements BankAccount {

    @Override
    public String getAccountType() { return "RA"; }

    ... whatever else ...
}

If BankAccount is a class then just make it an abstract method.

Upvotes: 13

Related Questions