Home arrow static arrow Java Programming [Archive] - Inheritance Question !!! please help
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 !!! please help
4 Duke Stars available
This topic has 8 replies on 1 page.

Posts:185
Registered: 4/23/04
Inheritance Question !!! please help  
Aug 5, 2004 9:47 AM



 
I have a superclass BankAccount and two subclasses SavingAccount and CreditAccount.
And i have a main class where i create my objects.

So i am trying to figure is it possible to make a class Customer and then from there to inherit all methods from SavingsAcc and CreditAcc.

Ex in main.
Customer John = new Customer(); //one object
John.SavingWithdraw(); //calls the method from SavingAccount
John.CreditWithdraw(); //calls the method from CreditAccount

If it is possible? How would i define my constructor from Customer?? Do i need to call the supers from superclasses (Saving,Credit,Bank)
 

Posts:37,103
Registered: 3/30/99
Re: Inheritance Question !!! please help  
Aug 5, 2004 9:55 AM (reply 1 of 8)



 
DIdn't you ask this question a few days ago?

Weren't you advised not to derive Customer from any of the Accounts? If not, consider yourself so advised now. DO NOT have Customer extend any of the Account classes. A customer is not an account. A customer can have references to his accounts, and/or accounts can have references to the customers that own those acocunts.

Okay, here are the rules for constructors--"ctors" because I'm lazy. Also, because I'm lazy, "super(...)" and "this(...)" mean any super or this call, regardless of how many args it takes, including those that take no args.

1) Every class has at least one ctor.

1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a
public MyClass() {...} 
if you want one.

1.3) Constructors are not inherited.

2) The first statement in the body of any ctor is either a call to a superclass ctor
super(...) 
or a call to another ctor of this class
this(...) 


2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor
super() 
as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

For more details on this and general info, see the following:

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)
Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]
Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].
 

Posts:24,036
Registered: 2/3/03
Re: Inheritance Question !!! please help  
Aug 5, 2004 9:58 AM (reply 2 of 8)



 
So i am trying to figure is it possible to make a
class Customer and then from there to inherit all
methods from SavingsAcc and CreditAcc.

In other words, it IS possible, it's just a terrible idea to do so.
 

Posts:185
Registered: 4/23/04
Re: Inheritance Question !!! please help  
Aug 5, 2004 10:05 AM (reply 3 of 8)



 
I am aware of Constructors and Supers at some stage i get them.

However what did you mean by this:

A customer can have references to his accounts, and/or accounts can have references to the customers that own those acocunts.

* Are you saying Customer class must have no extends at all?? If so how do i reference the accounts?? By supers??
 

Posts:957
Registered: 3/31/04
Re: Inheritance Question !!! please help  
Aug 5, 2004 10:05 AM (reply 4 of 8)



 

Customer c = new Customer();

hmmm. okay, you're screwed.

lol

You need to write your own methods that modify the balance either in the Customer or in some related BankAcct.

private void withdraw(int arg){    balance -= arg;}


or

public Customer{    SavingsAcct sa = new SavingsAccount();    int balance = sa.getBalance();     private void withdraw(int arg)    {        balance -= arg;    }}


Hmm. I still say you're screwed.
 

Posts:957
Registered: 3/31/04
Re: Inheritance Question !!! please help  
Aug 5, 2004 10:08 AM (reply 5 of 8)



 
u could try:

public Customer{      SavingsAcct sa = new SavingsAccount();  int balance = sa.getBalance();   private void withdraw(int arg)      {      sa.widthdrawCash(arg);  }}


This is all covered in the links that jverd posted.
 

Posts:24,036
Registered: 2/3/03
Re: Inheritance Question !!! please help  
Aug 5, 2004 10:08 AM (reply 6 of 8)



 
* Are you saying Customer class must have no extends
at all?? If so how do i reference the accounts?? By
supers??

By reference.

[url=http://www.artima.com/designtechniques/compoinh.html]Composition versus Inheritance[/url]

That may be a bit heavy for where you are right now, but give it a shot. You'll also want to check these out:

[url=http://java.sun.com/docs/books/tutorial/java/concepts/]Object-Oriented Programming Concepts[/url]
[url=http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html]Object-oriented language basics[/url]
[url=http://sepwww.stanford.edu/sep/josman/oop/oop1.htm]Don't Fear the OOP[/url]
 

Posts:185
Registered: 4/23/04
Re: Inheritance Question !!! please help  
Aug 5, 2004 10:16 AM (reply 7 of 8)



 
Oh thats my problem ... i didnt know that it is possible to create an object from subclasses and use it in a non-extended class. That is what i am looking for.

Thanks i am going to try that trick now. Oh and i might just read that Composition vs Inheritance ... composition seems a bit confusing, i will try to crunch it asap, because i will need it.
 

Posts:37,103
Registered: 3/30/99
Re: Inheritance Question !!! please help  
Aug 5, 2004 11:23 AM (reply 8 of 8)



 
A very simple example of the kinds of things yawmark and I are talking about. Dont take this literally for your project--it's just a generic example.
public interface Account {    long deposit(long amount); // returns new balance    long withdraw(long amoutn) throws InsufficientFundsException; // returns new balance    long balance(); // gets current balance    String id(); // gets account ID} public class CreditAccount implements Account {    private final Customer owner_;    private long balance_;    private String id_;     public CreditAccount(Customer owner, long initBal) {        owner_ = owner;        balance_ = initBal;        id_ = generateNewIDSomehow();    }     // Account methods} pubic class Customer {    // members for name, custId, etc.    private static final Map accounts_ = new HashMap(); //this customer's accounts, keyed by acct ID     public void addAccount(Account acct) {        accounts.put(acct.id(), acct);    }     public long withdraw(String accountID, long amount) throws InsufficientFundsException {        Account account = accounts_.get(accountID);        return account.withdraw(amount);    }    // etc.} 

A couple of things...

There's no error handling here. For instance, what if you try to withdraw from an account that doesn't belong to that customer.

When you make your other account class, you'll see a lot of duplicated code between it and CreditAccount, so you may want to have both of them extend some abstract class that implements some of that common functionality.

Does it really make sense to have CreditAccount and the other type inherit from a common interface or class? Or is the nature of a credit account different enough from the nature of the other account type that they should be in different hierarchies, that just happen to have some similarly named operations that do fundamentally different things?

Don't inherit just because you're going to have some methods with the same name. The parent and all its child classes must be fundamentally similar enough to make the ineritance make sense. Never inherit just to re-use code. You can do that with composition (like I did above by composing a Customer from accounts, and having the customer's withdraw method delegate to an account's withdraw method).
 
This topic has 8 replies on 1 page.