Harrychu
Harrychu

Reputation: 31

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

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

Upvotes: 2

Views: 1773

Answers (7)

Amit
Amit

Reputation: 13364

If you really want to convert a String to an object of some other class. You can have three possible solutions :-

  1. Create a Constructor which takes String as an argument and does the appropriate conversion to create the Object.

  2. Have a factory method i.e getInstance() which takes String parameter and returns an Object.

  3. You can have a method which can be used for the same purpose as above.

EDIT

Just forgot to mention that String is a final class which can't be modified or inherited in your application.

public Static SubClass getInstance(String str){
   SubClass obj = new SubClass(); 
   // ***the choice of constructor depends upon how u have created ur class
   //use str to form the SubClass object in following lines 
   return obj;
}

than you can use it as

SubClass newObj = SubClass.getInstance("String value here");

Upvotes: 3

dhblah
dhblah

Reputation: 10151

You can't do that because String class is final. Can you make composition instead of inheritance? http://www.artima.com/designtechniques/compoinh.html

Upvotes: 3

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

You should be having a concrete class(non abstract class) extending ConsolidateAccount lets say it being

public class ConcreteAccount extends ConsolidateAccount

Then you can use something like this.

ConsolidateAccount ownerAcct = new ConcreteAccount(array[3]);
new SavAcct(array[1],array[2],ownerAcct,Double.parseDouble(array[4]);

and create a constructor public ConcreteAccount(String str) in your ConcreteAccount class

Upvotes: 3

maksimov
maksimov

Reputation: 5811

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

Upvotes: 3

Marko Topolnik
Marko Topolnik

Reputation: 200148

You have an array of Strings and the constructor you are calling needs ConsolidateAccount at the third position. You need to instantiate a subtype of ConsolidateAccount based on that third String. There are no details to help you how to do that. Also, you are indexing the array starting from 1. Arrays are zero-based.

Upvotes: 0

Xavi López
Xavi López

Reputation: 27880

The java.lang.String class is final, so there's no way you can extend it. You could at most wrap it.

See Can I add new methods to the String class in Java?

Upvotes: 0

user606248
user606248

Reputation:

Not possible, String is final.

Upvotes: 2

Related Questions