Working through an assignment right now and could use help understanding a concept that is eluding me.
In particular, I have to create an ArrayList to hold two Account objects, and then use that list to collect input. Unfortunately when I try to store those objects into an ArrayList, one of them is giving me problems.
import java.util.ArrayList;
public class InheritanceTest {
private class Account {
//Constructor
Account() {
// Initialize balance
int balance = 0;
}
}
private class CheckingAccount extends Account {
}
private class SavingsAccount extends Account {
}
public static void main(String[] args) {
ArrayList<Account> Bank = new ArrayList<Account>();
CheckingAccount checking1 = new CheckingAccount();
Bank.add(checking1);
SavingsAccount savings = new SavingsAccount();
Bank.add(savings);
}
}
The command to instantiate a new CheckingAccount
object works fine, but once I plug the account into Bank.add()
the new CheckingAccount
object throws the "No enclosing instance of type InheritanceTest is accessible. Must qualify the allocation with an enclosing instance of type InheritanceTest (e.g. x.new A() where x is an instance of InheritanceTest)." error. I imagine there is some basic concept that I've not been able to find. I had all of my classes declared as static, and that allowed me to compile, but the things that I have read have made me think that's not the right way to go about it.