• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inheritence and the use of private & protected memebrs

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I am planning to implement some classes and subclasses ...
it would seem that I should plan on using "protected" access modifier instead of "private" when I'm at the top of the inheritence tree (in the parents or super classes).
This way, I can inherit these "protected" members in my sub classes and use them in objects instantiated from those subclasses right?
I know that using "private" access serves to encapsulate and protect members but it also seems to prevent sub classes from inheriting these same memebrs. IS SUING the PROTECTED modifier a good way to encapsulate yet allow good inheritence features?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, protected is a good way to go if you want access to subclasses and members in the same package. You can declare variables as 'private' though, and provide an accesor method to return that variables value. These are called getter/setter methods if you need to provide read/write access to the variables.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
[ July 16, 2002: Message edited by: david eberhardt ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


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.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


so I need to call super from the child class to get at the public method in parent which gets at that private variable?


// 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);
}

}

 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you don't need to redefine the accessors, as they get inherited, too:

Does that help?
[ July 16, 2002: Message edited by: Ilja Preuss ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]


after testing the above, the last line needed to be m.getAccountNumber() to avoid getting this from the compiler "JointAccount.java:14: non-static method getAccountNumber() cannot be referenced
from a static context
System.out.println("the new Joint Account number is " + getAcco
ntNumber() );
^
1 error
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, as the getAccountNumber is not static, you need an instance to refer to it - after all, only instances of Accounts have account numbers.
Does that help?
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any methodds called by the constructor should be either private or static or final. Not protected.
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic