Java Access Modifiers Tutorial


Java access modifiers help to control or restrict the accessibility of the members of a class. In day to day programming, we need to use access modifiers for a class, constructors, fields, and methods.

The below table summarizes all the java access modifiers and their visibility to other classes.


Access Modifier Class Package Subclasses Everywhere
private

default

protected

public


Details about Java access modifiers


  • private access modifier


    Java private access modifiers restrict accessing the members of the class from outside the class. All private members in the class can be accessed within the same class only.


        public class Bird {
            private String name;                //private fields
            private String color;
    
            private Bird(String name) {         //private constructor
                this.name = name;
            }
    
            private String getBirdName() {      //private method
                return name;
            }
    
            public Bird(String name, String color) {
                this(name);
    
                this.color = color;
            }
        }

    • private fields/ private class variables

      Java fields with private access modifiers restrict access from outside the class. In other words, private fields are accessible within the same class only.

          private String name;    //private field

    • private constructor

      Java constructor with private access modifiers restrict access from outside the class. In other words, a private constructor cannot use to create objects outside the class. However, we can call private constructors from other constructors or other methods which also in the same class.

          private Bird(String name) {         //private constructor
              this.name = name;
          }
          this(name);         //calling private constructor
          Bird bird = new Bird("Parrot");         //creating objects inside the same class

      Note: A famous example of the usage of a private constructor is a singleton design pattern.


    • private methods

      Java methods with private access modifiers restrict access from outside the class. The private methods cannot access from the outside the class but within the same class only.

          private String getBirdName() {      //private method
              return name;
          }
  • default access modifier


    A Java class member with default access modifiers only accessible within the same class and the same package. When we write code we dont write any keywords to to declare the member as a default member.


        class Bird {                    //default class
    
            String name;                //default fields
            String color;
    
            Bird(String name) {         //default constructor
                this.name = name;
            }
    
            String getBirdName() {      //default method
                return name;
            }
    
            public Bird(String name, String color) {
                this(name);
    
                this.color = color;
            }
        }

    • default class

      Java class with default access modifiers can only accessible within the same class or same package. The class will not visible to the other classes which are in different packages.

          class Bird {                    //default class

    • default fields/ default class variables

      Java fields with default access modifiers can only accessible within the same class or same package.

          String name;    //default field

    • default constructor

      Java constructor with default access modifiers can only accessible within the same class or same package.

          Bird(String name) {         //default constructor
              this.name = name;
          }
          this(name);         //calling default constructor
          Bird bird = new Bird("Parrot");         //creating objects inside the same package

    • default methods

      Java methods with default access modifiers can only accessible within the same class or same package.

          String getBirdName() {      //default method
              return name;
          }
  • protected access modifier


    A Java class member with protected access modifiers can be accessible by all subclasses even though they are from outside of the package. Apart from that, members who are in the same class and the same package also can access the protected members.


        public class Bird {
    
            protected String name;                //protected fields
            protected String color;
    
            protected Bird(String name) {         //protected constructor
                this.name = name;
            }
    
            protected String getBirdName() {      //protected method
                return name;
            }
    
            public Bird(String name, String color) {
                this(name);
    
                this.color = color;
            }
        }

    • protected fields/ protected class variables

      Java fields with protected access modifiers can only accessible by the members which are in the same class, the same package and all the subclasses.

          protected String name;    //protected field

    • protected constructor

      Java constructor with protected access modifiers can only accessible by the members which are in the same class, the same package and all the subclasses.

          protected Bird(String name) {         //protected constructor
              this.name = name;
          }
          this(name);         //calling protected constructor
          Bird bird = new Bird("Parrot");         //creating objects within all subclass

    • protected methods

      Java methods with protected access modifiers can only accessible by the members which are in the same class, the same package and all the subclasses.

          protected String getBirdName() {      //protected method
              return name;
          }
  • public access modifier


    A Java class member with public access modifiers can be accessible from the everywhere.


        public class Bird {                    //public class
    
            public String name;                //public fields
            public String color;
    
            public Bird(String name) {         //public constructor
                this.name = name;
            }
    
            public String getBirdName() {      //public method
                return name;
            }
    
            public public Bird(String name, String color) {
                this(name);
    
                this.color = color;
            }
        }

    • public class

      Java class with public access modifiers is visible to everywhere.

          public class Bird {    //public class

    • public fields/ public class variables

      Java fields with default access modifiers can be accessible from the everywhere.

          public String name;    //public field

    • public constructor

      Java constructor with default access modifiers can be accessible from the everywhere.

          public Bird(String name) {         //public constructor
              this.name = name;
          }
          this(name);         //calling public constructor
          Bird bird = new Bird("Parrot");         //creating objects everywhere

    • public methods

      Java methods with default access modifiers can be accessible from the everywhere.

          public String getBirdName() {      //public method
              return name;
          }

  • Summary of where to apply access modifiers

    Access Modifier Class Constructor Method Field
    private

    default

    protected

    public


<< Java Overview      Java Objects and Classes >>