File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes difference between protected and private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between protected and private" Watch "difference between protected and private" New topic

difference between protected and private

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
what is difference between protected and private?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

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!

    [Jess in Action][AskingGoodQuestions]
    Adam Brown

    Joined: Jan 08, 2004
    Posts: 11
    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'.
    GeeCON Prague 2014
    subject: difference between protected and private