简体   繁体   中英

How do I create an ArrayList filled with different types of objects?

I have different classes for a bank system. These classes are;

  • SavingsAccount
  • StudentAccount
  • NormalAccount

Now I want to create an ArrayList and populate it with objects of all these different classes, eg first element being studentaccount1, second element being, savingsaccount 2... and so on.

So I tried using the object type:

List<Object> accountList = new ArrayList<>();

But with this approach, for some reason I'm unable to access the values stored in the class variables, for example:

accountList.add(saving1);

accountList.get(saving1) doesn't bring up the methods declared in those classes.

So how could I fix this, or implement it in some kind of another way?

EDIT:

I forgot to mention that Normal account is the parent class and both StudentAccount and SavingsAccount are subclasses.

Therefore I realized that I can just declare the ArrayList as the NormalAccount type (parent class type) instead and achieve what I wanted.

Create an interface Account with common methods and make all these accounts implement it.

public class SavingsAccount implements Account { ... }

Then create a list of the type implementing this interface.

List<Account> accountList = new ArrayList<>();

I don't recommend you to create a marker interface just for this purpose. It's a bad idea. An interface should have public methods.

Alternatively, the Account can be an (abstract) class itself and these specific accounts would extend from it - include there the common functionality to avoid the code duplicity. It depends what exactly do you want to achieve and which one fits your design.

Edit:

You missed to disclose us a quite significant fact that NormalAccount is the parent of the others. Thus the solution is quite simple then:

List<NormalAccount> accountList = new ArrayList<>();

Your class have suffix _Account so I guess they have similar fields.

Make a super class for them, either interface or class:

public class Account {
}

public class StudentAccount extends Account {
}

Then declare your list as:

List<Account> accountList = new ArrayList<>();

Account normal = new NormalAccount();
accountList.add(normal);

You can't access the methods of the classes. Instead, you can only access methods of Object class in your example.

I think you can put common methods declarations in an interface or abstract class, then implement them.

I forgot to mention that Normal account is the parent class and both "StudentAccount" and "SavingsAccount" are subclasses.

So can I just declare the ArrayList as a "NormalAccount" type and then be able to access the variables?

Yes, exactly. You can define it like,

List<NormalAccount> accountList = new ArrayList<>();

When you use the Object class, during retrieval of data, you need to cast the retrieved data to be able to use the methods. Try something like this:

(SavingsAccount) accountList.get(saving1)

you will be able to access the methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM