 | | From: | Andy Redman | | Subject: | Protected member variable vs. protected accessor methods | | Date: | Thu, 20 Jan 2005 10:00:40 +0000 (UTC) |
|
|
 | Hi
I have a class (Java) with a private member variable. I now need to extend the class to add a sub class which adds a new function. The new function needs access to the member variable; at this moment it does not need to alter the variable. As I see it I have two options, a - make the member variable protected or b - add protected accessor methods to the super class. Is there any general rule about which approach to take.
Cheers,
Andy
|
|
 | | From: | Daniel T. | | Subject: | Re: Protected member variable vs. protected accessor methods | | Date: | Thu, 20 Jan 2005 15:42:45 GMT |
|
|
 | Andy Redman wrote:
> I have a class (Java) with a private member variable. I now need to > extend the class to add a sub class which adds a new function. The new > function needs access to the member variable; at this moment it does > not need to alter the variable. As I see it I have two options, a - > make the member variable protected or b - add protected accessor > methods to the super class. Is there any general rule about which > approach to take.
The general rule is called the rule of least exposure. The less you expose, the less likely some outside force will break what you have.
Provide the getter for the variable, but do not provide a setter.
|
|
 | | From: | Andy Redman | | Subject: | Re: Protected member variable vs. protected accessor methods | | Date: | Thu, 20 Jan 2005 17:04:09 +0000 (UTC) |
|
|
 | On Thu, 20 Jan 2005 15:42:45 GMT, "Daniel T." wrote:
>Andy Redman wrote: > >> I have a class (Java) with a private member variable. I now need to >> extend the class to add a sub class which adds a new function.
snip!
>The general rule is called the rule of least exposure. The less you >expose, the less likely some outside force will break what you have. > >Provide the getter for the variable, but do not provide a setter.
That's more or less the conclusion we came to - good to get a second opinion.
Cheers Andy
|
|
 | | From: | Ilja Preuß | | Subject: | Re: Protected member variable vs. protected accessor methods | | Date: | Thu, 20 Jan 2005 22:39:09 +0100 |
|
|
 | Andy Redman wrote: > On Thu, 20 Jan 2005 15:42:45 GMT, "Daniel T."
>> Provide the getter for the variable, but do not provide a setter. > > That's more or less the conclusion we came to - good to get a second > opinion.
Well, here is a third: full agreement! ;)
Cheers, Ilja
|
|