Home arrow static arrow Java Programming [Archive] - Inheritance question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Inheritance question
3 Duke Stars available
This topic has 1 reply on 1 page.

Posts:185
Registered: 4/23/04
Inheritance question  
Aug 1, 2004 8:56 AM



 
I have a superclass Account
and two seperate subclasses Savings and Credit

Both Savings and Credits extends Account

In my main i have an object like this:
<code>
CreditAccount customer1 = new CreditAccount();
SavingAccount customer2 = new SavingAccount();
</code>

I was just wondering how could i make it as a unified customer1 account for both Credit and Savings without making seperate objects for each account.
 

Posts:8,813
Registered: 10/4/00
Re: Inheritance question  
Aug 1, 2004 9:12 AM (reply 1 of 1)



 
I assume the reason you made CreditAccount and SavingAccount is because they act differently. So you shouldn't make them the same object.

Maybe what you want is to have one customer be able to have multiple accounts. Make a customer class which can hold the name, address,... and a set of accounts. Maybe in an ArrayList if you've learned that yet. If you haven't, maybe you could assume a customer can only have one credit and ond saving account. Here's a start.
public class Customer {    String name;    CreditAccount cAcct;    SavingAcccount sAcct;    public Customer(String name) { this(name,null, null); }    public Customer(String name, CreditAccount cAcct, SavingAccount sAcct) {        this.name=name;        this.cAcct=cAcct;        this.sAcct=sAcct;    }    public String getName() { return name; }    public CreditAccount getCreditAccount() { return cAcct; }    public SavingAccount getSavingAccount() { retunr sAcct; }}

 
This topic has 1 reply on 1 page.