Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keeping variables private in a bean.

 
Ed Gandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got a bean CraneAsset, which extends Asset.
"Asset" has a property called "assetNum" and appropriate get/set methods.
When I create a CraneAsset bean and use getAssetNum, I got a message that "assetNum" is a private variable in the superclass "Asset". So I went back to the "Asset" and changed "assetNum" to "protected".
But making it "protected" to get "assetNum" to inherit down to the subclass properly defeats the whole purpose of keeping all bean variables "private". Now another class in the same package can access the variable with Classname.assetNum.
Is there a way around this dilemma?
thanks, - ed g.
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keep the variable private & get the contents of the variable into the subclass by calling the appropriate get method of the variable in the superclass.
Correct me anybody if I am wrong.
Originally posted by Ed Gandy:
I have got a bean CraneAsset, which extends Asset.
"Asset" has a property called "assetNum" and appropriate get/set methods.
When I create a CraneAsset bean and use getAssetNum, I got a message that "assetNum" is a private variable in the superclass "Asset". So I went back to the "Asset" and changed "assetNum" to "protected".
But making it "protected" to get "assetNum" to inherit down to the subclass properly defeats the whole purpose of keeping all bean variables "private". Now another class in the same package can access the variable with Classname.assetNum.
Is there a way around this dilemma?
thanks, - ed g.

 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes Roopa is right. Here's a little example I did to be sure it worked:

You can declare Sub.x private in your bean. I couldn't here 'cos I wanted Super to print it out for debugging.

Bye,
------------------
Terry Doyle
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Terence Doyle (edited October 12, 2001).]
 
Ed Gandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. i will implement the suggestion. - ed g
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic