Access modifier in java

Access modifiers:

Access modifiers are keywords used for defining accessibility of classes, methods and data members.

Types of access modifier.

  1. Private.
  2. Default
  3. Protected
  4. Public

Private:

Data members, methods and constructors that are declared with private access modifier can be accessed into that class only.

Example:

PrivateAccessModifier.java

/**
 * This program is used to show that private members
 * of a class can be accessed in that class only
 * @author W3spoint
 */
class Student {
      //private members of the class
      private int rollNo = 5;
 
      private void showRollNo(){
              //rollNo which a private data member is
              //accessible in that class.
              System.out.println("RollNo = " + rollNo);
      }
}
 
public class PrivateAccessModifier {
       public static void main(String args[]){
              //creating Student class object
              Student obj = new Student();
 
              //compile time error because private members
              //of a class can be accessed in that class only.
              System.out.println(obj.rollNo);
 
              obj.showRollNo();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problems:
The field Student.rollNo is not visible
The method showRollNo() from the type Student is not visible
at com.w3spoint.business.PrivateAccessModifier.main
(PrivateAccessModifier.java:24)

Download this example.

Note: A class can have a private constructor but we cannot create an instance of that class from outside the class.

PrivateConstructor.java

/**
 * This program is used to show that we cannot create an instance
 * of that class from outside the class if constructor is private.
 * @author W3spoint
 */
class Student {
      //private constructor of the class
      private Student(){                               
 
      }
 
      public void show(){
             System.out.println("Hello w3spoint.com");
      }
}
 
public class PrivateConstructor {
       public static void main(String args[]){
               //compile time error in creating Student class object
               //because of private constructor.
               Student obj = new Student();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The constructor Student() is not visible
at com.w3spoint.business.PrivateConstructor.main
(PrivateConstructor.java:22)

Download this example.
Note: Classes and interfaces can’t be private except nested classes.

Default:

Classes, data members, methods and constructors that are not declared with any access modifier are treated as default. They can be accessed into all classes within the same package only.

Example 1:

DefaultAccessModifier1.java

/**
 * This program is used to show that members of the class
 * of a class can be accessed in the package level only.
 * @author W3spoint
 */
class Student {
      //private members of the class
      int rollNo = 5;
 
      void showRollNo(){
           System.out.println("RollNo = " + rollNo);
      }
}
 
public class DefaultAccessModifier1 {
        public static void main(String args[]){
               //creating Student class object
               Student obj = new Student();
 
               //No compile time error because members of the class
               //of a class can be accessed in that package but can't be
               //accessed outside the package.
               System.out.println(obj.rollNo);
               obj.showRollNo();
         }
}

Output:

5
RollNo = 5

Download this example.

Example 2:

Display.java

package com.w3spoint.display;
/**
 * This class is used to print a line.
 * @author W3spoint
 */
class Display {
      void display(){
           System.out.println("Hello w3spoint.com");
      }
}

Test.java

package com.w3spoint.test;
import com.w3spoint.display.*;
/**
 * This program is used to show that default members of the class
 * are not accessible from outside the package.
 * @author W3spoint
 */
public class Test {
        public static void main(String args[]){
               //compile time error because default members of the class
               //are not accessible from outside the package.
               Display obj = new Display();
               obj.display();
       }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problems:
The type Display is not visible
The type Display is not visible
The type Display is not visible
at com.w3spoint.test.Test.main(Test.java:13)

Download this example.

Protected:

Data members, methods and constructors that are declared with protected access modifier can be accessed into all classes within the same package and only in subclasses outside the package.

Example 1:

Display.java

package com.w3spoint.display;
/**
 * This class is used to print a line.
 * @author W3spoint
 */
public class Display {
       protected void display(){
                 System.out.println("Hello w3spoint.com");
        }
}

Test.java

package com.w3spoint.display;
package com.w3spoint.test;
 
import com.w3spoint.display.*;
/**
 * This program is used to show that protected members of the
 * class are accessible from outside the package in subclasses.
 * @author W3spoint
 */
public class Test extends Display{
        public static void main(String args[]){
               Test obj = new Test();
 
               //No compile time error because protected members
               //of the class are accessible from outside the
               //package in subclasses.
               obj.display();
         }
}

Output:

Hello w3spoint.com

Download this example.

Example 2:

Display.java

package com.w3spoint.display;
/**
 * This class is used to print a line.
 * @author W3spoint
 */
public class Display {
       protected void display(){
                 System.out.println("Hello w3spoint.com");
       }
}

Test.java

package com.w3spoint.test;
 
import com.w3spoint.display.*;
/**
 * This program is used to show that protected members of the class
 * are not accessible from outside the package in non-subclasses.
 * @author W3spoint
 */
public class Test {
       public static void main(String args[]){
              Display obj = new Display();
              //compile time error because protected members
              //of the class are not accessible from outside
              //the package in non-subclasses.
              obj.display();
        }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The method display() from the type Display is not visible
at com.w3spoint.test.Test.main(Test.java:15)

Download this example.
Note: Classes and interfaces can’t be protected except nested classes.

Public:

Classes, data members, methods and constructors that are declared with public access modifier can be accessed everywhere.

Example:

Display.java

package com.w3spoint.display;
/**
 * This class is used to print a line.
 * @author W3spoint
 */
public class Display {
       public void display(){
              System.out.println("Hello w3spoint.com");
       }
}

Test.java

package com.w3spoint.test;
 
import com.w3spoint.display.*;
/**
 * This program is used to show that public members of the class
 * are accessible everywhere.
 * @author W3spoint
 */
public class Test {
       public static void main(String args[]){
              Display obj = new Display();
              //public members of the class accessible everywhere.
              obj.display();
       }
}

Output:

Hello w3spoint.com

Download this example.

Interview Questions on Access Modifiers

Next Topic: Static import in java with example.
Previous Topic: Package in java with example.

 

Content Protection by DMCA.com
Please Share