4

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?

3
  • What is the purpose of knowing which class it is? Commented Nov 23, 2015 at 21:00
  • use OOP and define getAccountType in each of your Account classes Commented Nov 23, 2015 at 21:01
  • Check this out Commented Nov 23, 2015 at 21:03

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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;
    }
}

5 Comments

The accountType shouldn't be a member, as it's associated with the class, not with instances of the class.
@WillieWheeler, true, for absolutely what OP is asking, just a method is good enough. But if the accountType is an attribute of each account, it's best a member of the generalization.
What you suggest is not overloading — it is overriding. And if you're going to require getAccountType to be overridden by each concrete account class, you should make it public abstract String getAccountType();
-1 as this solution permits different account instances of the same type to have different account type strings. Also every account instance has to store the same string, which isn't necessary.
@WillieWheeler thanks for the comment, but I didn't see that requirement. Moreover, this is an idea to solve, not a solution to copy paste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.