• 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

Access Modifiers in Java

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can anyone please explain me the access modifiers with simple examples.
i am very much confused in particular when using the protected and default ..............when to use the dot operator and when to access directly.
please help me clear my doubts.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recommended reading
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ruchita mahajan wrote:hi,
can anyone please explain me the access modifiers with simple examples.
i am very much confused in particular when using the protected and default ..............when to use the dot operator and when to access directly.
please help me clear my doubts.



Please specify clearly your problem...........
You can acess only class non-static member methods and member variables directly.
Protected members can be accessed by class members as well as derieved class members.
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Malaviya wrote: . . .
You can acess only class non-static member methods and member variables directly.
Protected members can be accessed by class members as well as derieved class members.

That looks incorrect to me. Maneesh has already given you a useful link.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this is what you are looking for?

In short :

Default (if you don't specify an access modifier) - anything in the same package can access it
public - anything that uses your code will have access, so if class A imports your class, it can see the public accessors
private - only the class its in can access it
protected - anything that extends your class. So if you have a protected method in class A, and class B extends class A, class B will have access to the protected methods

Its always advised to use the most restrictive access possible, not sure of the real reasons why, other than to expose as little as possible
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, you forgot one thing: members that are protected are not only visible in subclasses, but also in classes in the same package.

As already said, the page that Maneesh provided a link to has a very clear description and overview table that explains it.

(And static does not have anything to do with access control).
 
ruchita mahajan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the replies.The link that Maneesh has sent is really very helpful
 
It's feeding time! Give me the food you were going to give to this tiny ad:
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