posted 22 years ago
Hello,
I dont know exactly when to use the protected modifier.
I read several posting on several pages and as far as i can see, there are 2 main opinions
1)
protected should only be used when a subtype
needs to access parts of its supertype for
efficiency reasons
By default, a subtype should access its supertype
only through the public interface
2)
when you expect that you will have subclasses in different packages to be able to access things that most classes in your system shouldn't.
I can understand this 2 points.
In point 1 data hiding is the reason for that
In point 2 inherance is the reason for that
Which one is right approach, what do u think about this ?
I always make all members package scope(default)
and if there exists derived classes in other packages i make the affected attribute protected only if i want to avoid manupiulation i make
the affected attribute private.
Now we have a new coding convention and there s written members should always be private and the access should result from a set/getter for it (i alyway write a set/getter for my package/protected scope attributes)
This approach is like point 1)
Now i am programming like point 2)
What is the right/better way to do it?Wht your opinion about it ?
Thank you very much,
Holger