What is the difference between public, protected, package-private and private in Java?

Dung Do Tien Sep 24 2021 108

I'm a newbie in Java since a week, I want to clear rules about when to use each of the access modifiers, namely default (package private) public, protected and private when doing with class and interface and dealing with inheritance?

Anyone can explain for me what difference between them and when we should use them. 

public class StudentInfo{
   private _id;
   private _fullname;
   public string Fullname{
      set{ _fullname = value;}
      get{ return _fullname; }
   }
}

Thank you for any suggestions.

Have 4 answer(s) found.
  • N

    Nam Vo Sep 24 2021

    You can follow table shows the access to members permitted by each modifier.

    Modifier Class Package Subclass World
    public Y Y Y Y
    protected Y Y Y N
    no modifier Y Y N N
    private Y N N N
  • đ

    đặng thái sơn Sep 24 2021

    1. Private

    As you might think, only the class in which it is declared can see it.

    2. Private package

    It can only be seen and used by the package in which it was declared. This is the default in Java (which some may think is a bug).

    3. Protected

    The Private + package can be seen by subclasses or members of the package.

    4. Public

    Everyone can see it.

  • o

    okyanus gürgencioğlu Sep 24 2021

    ____________________________________________________________________
                    | highest precedence <---------> lowest precedence
    *———————————————+———————————————+———————————+———————————————+———————
     \ xCanBeSeenBy | this          | any class | this subclass | any
      \__________   | class         | in same   | in another    | class
                 \  | nonsubbed     | package   | package       |    
    Modifier of x \ |               |           |               |       
    ————————————————*———————————————+———————————+———————————————+———————
    public          |       ✔       |     ✔    |       ✔       |   ✔  
    ————————————————+———————————————+———————————+———————————————+———————
    protected       |       ✔       |     ✔    |       ✔       |   ✘   
    ————————————————+———————————————+———————————+———————————————+———————
    package-private |               |           |               |
    (no modifier)   |       ✔       |     ✔    |       ✘       |   ✘   
    ————————————————+———————————————+———————————+———————————————+———————
    private         |       ✔       |     ✘    |       ✘       |   ✘    
    ____________________________________________________________________

    You can see table above.

  • D

    Dorian G. Ramos Sep 24 2021

    You can short understand as below:

    • public : available from anywhere.
    • protected : Available to classes in the same package and to subclasses in any package.
    • default (no modifier specified): available to classes of the same package.
    • private : only available within the same class.
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close