#Trying to be smart with switch case based on extended classes.

1 messages · Page 1 of 1 (latest)

grand notch
#

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).

wicked remnantBOT
#

This post has been reserved for your question.

Hey @grand notch! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

gritty crypt
#

It's weird of you to wish that the individual classes don't express their differences. What's the point of individual classes then?

grand notch
#

I just want interestRate to be final
and it needs to be assigned a value in the base constructor for that apparently (at least my IDE isn't happy otherwise)

gritty crypt
#

Indeed. So just make it a parameter of the base constructor

grand notch
#

I don't want it to be a parameter, they're fixed values based on the specific class

gritty crypt
#

And so therefore, these classes will pass the parameter they each want, without anyone else being given the option that they pass anything else

grand notch
#

I don't understand, if I pass it as a parameter why won't anyone else be able to just pass a different value?

gritty crypt
#

They will if they create their own new Account().
They won't if they do anything else because then it's not them who call the constructor of Account.
Notably they won't if they create a new SavingsAccount nor a new CreditAccount

grand notch
#

I think I understand now
is there a way to assure that classes which extend Account have their own constructor?

gritty crypt
#

Well, the parent class having a parameter is a hard requirement of that, yes

grand notch
#

alright great, thanks!

wicked remnantBOT
# grand notch alright great, thanks!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

grand notch
gritty crypt
#

Pretty sure you can't switch on a Class