Bosun (SCJP, SCWCD).
So much trouble in the world -- Bob Marley
Originally posted by david eberhardt:
Bosun, thanks for the reply. Let me try and make my question more precise:
question: when I create a JointAccount object, I know I can directly access the accountName field using a object reference from this subclass - BUT how do I add an accountNumber to this JointAccount since I did not inherit the private field?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
You are confusing something here: JointAccount *is* inheriting the private field, it's just not *visible*. If you need to access it, it would probably be better to provide protected accessor methods instead of accessing the field directly. This way the super class always gets notified when someone tries to access the value, making it much easier to later introduce techniques like lazy initialization, caching etc.
// let's say I have
package david.accounts1;
public class CheckingAccountSingle {
private int accountNumber;
protected String accountName;
// constructors and getter/setter methods here
public setaccountNumber(int a) {
// some audit code here checking a
accountNumber = a;
}
}
// let's say I extend the class above in another package
package david.accounts2;
import david.accounts1;
public class JointAcount extends CheckingAccountSingle {
// constructors and getter/setter methods here}
public setaccountNumber(int a) {
super.setaccountNumber(a);
}
}
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by david eberhardt:
Ilja Preuss
re:
so if in the JointAccount class,
can I do this and set it's AccountNumber thus:
*** or should my last line above read: System.out.println("the new Joint Account number is " + m.getAccountNumber() );
[ July 16, 2002: Message edited by: david eberhardt ]
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|