0

I'm writing a code in Java using abstract classes for the first time and I have a problem. I have a list (array) of users, each of them is an object of userClass. I have created an abstract class 'account' and 2 classes which extends it 'basicAccount' and 'premiumAccount' and I would like to create basic or premium account for every user depending on an input. My problem is that I don't know how to implement userClass, so it would create object of basic or premium class depending on a string 'accountType' Example:

public class userClass {
private String email;
private String accountType;
private account account;

public userClass(String email, String accountType){
    this.setEmail(email);
    this.setAccountType(accountType);

    if(accountType.equals("Basic")){
        basicAccount account = new basicAccount();
    }
    else if(accountType.equals("Premium")){
        premiumAccount account = new premiumAccount();
    }
}

Could you please help me? Can I put those if's in userClass method?

1
  • Actually your code is mostly correct. You just need to remove account classes(premiumAccount and basicAccount) in front of 'account' variables. Just initilaze them as related sub-class. Commented Apr 2, 2016 at 0:16

2 Answers 2

2

A very important concept in Object-oriented Programming is polymorphism. Polymorphism allows you to use an object A as if it were an instance of another type B if A and B follow any one of these rules

  • B is a class and A's type is a subclass of B
  • B is an interface and A's type implements B

In your case, the first rule applies. Both basicAccount and premiumAccount are a subclass of account. Thus, you can use your premium account and basic account objects as account objects! So just ASSIGN the newly created object to the account field!

if(accountType.equals("Basic")){
    basicAccount account = new basicAccount();
    this.account = account; // This works! Don't worry!
}
else if(accountType.equals("Premium")){
    premiumAccount account = new premiumAccount();
    this.account = account;
}

A side effect, though, is that after using basicAccount and premiumAccount objects as accounts, you cannot access the methods of basicAccount and premiumAccount by directly accessing this.account.

If you really want to do so, try this method:

Say you have a doStuff method defined in basicAccount and you want to call it:

this.account.doStuff();

That does not work because this.account is of type account and account does not define a method called doStuff.

What you need to do is check whether the account field is of type basicAccount:

if (this.account instanceof basicAccount)

In the if statement, cast this.account to basicAccount and call the method:

((basicAccount)this.account).doStuff();
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this would probably do it:

public class UserClass{
    Account account;

    public UserClass(String accountType){
         if(accountType.equals("Basic"){
              this.account = new basicAccount();
         } else {
              this.account = new premiumAccount();
         {
    }
}

Comments

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.