• 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

what's the purpose of private and protected access modifiers?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to understand the user of 'private' and 'protected'access modifiers and the default access control.

I know how it works.
PRIVATE: When we make a variable private it is not visible outside the class.
PROTECTED: When we make 'protected', only the class that extents it, could have the access.

Why everything can't be just public. If it is for security, it is the just programmer who knows what he is doing with the program. Then why to make it private or protected.

If we make it private or protected, does it going to take less memory.

Someone please tell when to go for private, protected and public.

Thanks
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected can be seen by classes in the same package and subclasses, no matter what package. package modifier is more restrictive - only classes in the same package can see it.
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raf Szczypiorski,

Thanks for your reply. Sorry I didn't mention fully for PROTECTED. I agree with you.

PROTECTED: Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.

My question is "Why everything can't be just public? What is the signficance it is going to make. Please tell the actual use of PRIVATE and PROTECTED rather than technically how it works. Why access modifiers. Why we should use them?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is that if you make everything public then programs will get a lot more complex. Because now instead of 1 way of doing something you now have 20.
And direct access is a lot less flexible then delegated access. It also allows to force certain conditions or actions to happen before access. Read about encapsulation.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If everything was public, everybody would have access to the internals of your class. This can be bad, preventing you from changing the implementation without breaking other code. If you keep everything as private as possible and expose only the publicly available 'interface' (mind, I don't mean Java interfaces here, yet), you make sure all your users can only see these member, which gives you the liberty of reimplementing some stuff at some point (to make it better, faster, more error proof or simply different) and still be sure that clients of your class will continue to work as before (I am assuming that when you reimplement the internals, you remain compatible when it comes to the public behavior / contract). In Java you can go to the extreme and just define interfaces for clients of your code, and their implementations can be completely hidden and actually irrelevant to the clients. The general rule is to code against behavior, not implementation.
An example: ArrayList<String> list = new ArrayList<String>(); // *
here you use list by its public methods. If you had access to the internal array and your code made us of it, it would be impossible (**) for the authors of this class to reimplement it, for example, using direct byte buffers or whatever (just an example). With protecting the internals, they are leaving themselves a door open for future reimplementations.

Having said that, there are languages that have public as the default, for various reasons. For example, Python doesn't even have anything less the public, you can only imitate private with weird naming conventions / trick; Scala, a very powerful and popular language for the JVM has public by default.

* This example can be written better: List<String> list = new ArrayList<String>() as in the 'always code against interfaces' rule, or even the list could be created by a factory or injected.
** This is not actually true - there exists a very powerful set of classes for Java - Reflection. With these classes you can access whatever you want, whenever you want, even private methods and fields, relatively easy. But that's outside of scope.

Hope this can be helpful.
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raf Szczypiorski,

Thanks a lot for your time.
It helps me a lot now.

Regards
Loganathan Karunakaran
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wouter Oet,

Thanks for your time and explanation.

Regards
Loganathan Karunakaran
reply
    Bookmark Topic Watch Topic
  • New Topic