• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Default and Protected Access Modifier

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body give some examples where we will use default,protected access modifier.
Thanks
Ravi
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravi,
Welcome to JavaRanch.
An example of Default access is easy: it is package level access. Consider making a database program where you have the actual database, a server program and a client program. You might create several packages to hold the classes that perform these functions, such as:

Now in the "database" package, I have 3 classes: file reader/writer; record manager; and lock manager. These do distinct jobs - the file reader is only concerned about putting data into the file in the right spot and getting it out again. The record manager is concerned with the concept of a record (breaking it into fields, allowing creates, reads, updates, and deletes). The lock manager is only concerned with locking and unlocking logical records.
I only want other classes to use the record manager to do all their work - it will call the lock manager and the file reader / writer when necessary. It could be potentially disasterous if some other class called my file reader / writer and overwrote some of my data without having been able to verify that the record was locked properly and the data was correct.
So therefore I want to make sure that any class in the database package can see my locking functions, but classes in other packages cant. Simple solution: default access!
-----
An example for protected access is a bit harder.
One not very useful example is debugging code. You have written a class that works, but in the process of building it you had to create some "interesting" debugging routines (similar to "toString()" methods). You do not want these to be available to the normal user of your class, but you think it would be very useful to anyone who extends your class. You cannot make it default access otherwise the extending code would also need to be in the same package. You cannot make it public or it would become available to every programmer and they probably dont want to be bothered with yet another method that they could but never will call. So that leaves protected as the best choice.
Take a look at JButton.paramString() for an example of this usage.
Another example is the Properties class - it has a "defaults" field which is protected - classes using Properties are not even aware that the defaults exist, but a class extending Properties would be able to access it.
Regards, Andrew
 
Ravi Shankar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Andrew
Rgds
Ravi
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic