• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access Modifiers in Java  RSS feed

 
ruchita mahajan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Anurag Malaviya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all for the replies.The link that Maneesh has sent is really very helpful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!