I'm still not used to working with abstraction in Java, so there's probably a better and entirely different way of doing this, but here's what I want:
I have an abstract class Account, and for now two classes SavingsAccount and CreditAccount that both extend Account.
In Account I have a variable interestRate, which I want to be final but which is also different for each account type.
So, I want to specify different values depending on the class being instantiated in the abstract constructor, so I did this:
public Account() {
switch (this.getClass()) {
case SavingsAccount.class -> this.interestRate = new BigDecimal("1.2");
case CreditAccount.class -> this.interestRate = new BigDecimal("0.5");
}
}
But now I'm getting the error below, can someone explain why this is wrong?
both SavingsAccount and CreditAccount definitely extends Account, so I don't really understand the problem with this.
P.S after writing the code it did start feeling a bit illegal, so I'm almost certain this isn't the way you're supposed to go about things in this scenario, but I don't really know what alternative I have because I'd prefer not having to specify constructors in the individual classes (though I will if that's the only/best option).