• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Choosing the right modifier

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I don't like default scope, so few people really understand what it means.
Given that, I tend to make all my data members private unless there is a real good reason not to. I make the great majority of my methods public, with only the few which are dependent on particular internal state as private.
If I find that I really need to inherit the use of a particular data member or private method, then I will make it protected, but I regard this as a little bit risky.
 
reply
    Bookmark Topic Watch Topic
  • New Topic