I want to convert a String to an subclass data type, how can it be done? or it is possible?
I have a abstract class Acct
A public abstract class SinAcct extends Acct
A public class SavAcct extends SinAcct
In SavAcct, there is a constructor
public SavAcct(String acctNo, String name, ConsolidateAccount ownerAcct, double lastMonthBal){
super(acctNo,name,ownerAcct,lastMonthBal);
}
An abstract class ConsolidateAccount extends Account
I want to new a SavAcct,
new SavAcct(array[1],array[2],array[3],Double.parseDouble(array[4])
but it is error The constructor SavAcct(String, String, String, double) is undefined
anyone can help me? pls
Just to be sure you're not going on a wrong path, instead of adding a new constructor which will essentially need to call super(acctNo,name,ownerAcct,lastMonthBal); like this existing constructor, why don't you otherwise try and create or look up ConsolidateAccount instance using your array[3] key?
E.g.
ConsolidateAccount consolidateAccount = new ConcreteConsolidateAccount(array[3]);
new SavAcct(array[1],array[2],consolidateAccount,Double.parseDouble(array[4]);
Where ConcreteConsolidateAccount is a concrete class extending ConsolidateAccount.
Looks like a more sensible thing to do.
Of course I don't know about logic around ConsolidateAccount, or even if it has a constructor that takes a String, but this is just to give you an idea, because it would appear that you need to call the constructor of the class that SavAcct is extending (this is indicated by the super call).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With