• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between protected and private

 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is difference between protected and private?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are four access levels in Java.
  • private things can be directly accessed only within the class that defines them.
  • default (i.e., no access modifier) things can be accessed by any code in the same package as the defining class.
  • protecte is like default, except subclasses outside of the defining package can also access the member.
  • public members are accessible to all code everywhere.


  • Many people are surprised to learn that "protected" is actually less protected than the default!
     
    Adam Brown
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As an edition to Mr. F-H's explanation on protected members, it should also be noted that if another subclass 'B' were to extend from subclass 'A' already extended from a superclass containing protected members (while existing in another package), those protected members would only be seen by the subclass 'A'. In a sense, those inherited protected members assume private visibility within subclass 'A' and cannot be seen by subclass 'B'.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic