Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to convert a String into an subclass data type?

Tags:

java

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

like image 573
Harrychu Avatar asked Jul 08 '26 15:07

Harrychu


1 Answers

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

like image 163
maksimov Avatar answered Jul 11 '26 04:07

maksimov