Access Modifiers in Java – Types and Examples


1. Objective

In this tutorial for Access Modifiers in Java, Java language is one of the most popular language for programming. Here, we are going to learn about the various access operators in Java. They are used for restricting the scope of any Java class, object, function, or a constructor. So, let us start with this tutorial.

Access Modifiers In java

Access Modifiers In java

2. What is Access Modifiers in Java

Java Modifiers are used to define access to various Java classes and objects, Java functions, and Java constructors. They are also used to change the meaning of any keyword in Java.

There are 4 types of access modifiers in Java, namely

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

3. Types of Access Modifiers in Java

a. Default Access Modifiers in Java

The default Java Modifier is used to define access when no other modifier is specified for the class or function, with this modifier they are accessible only within the same package. Before discussing the example, let’s revise Java Syntax and Java Variables.

Example

//Java program to illustrate default modifier
package p1;
//Class Example is having Default access modifier
class Example
{
  void display()
  {
    System.out.println("Hello World!");
  }
}

 

//Java program to illustrate error while using class from different package with default modifier
package p2;
import p1.*;
//This class is having default access modifier
class ExampleNew

  {
     public static void main(String args[])

    {
                    //accessing class Example from package p1
                     Example obj = new Example();
                     obj.display();
    }
  }

Output 

Compile time error

b. Private Access Modifiers in Java

The keyword private Java Modifier is used to define access to a Java datatype or method, with this it is only accessible in the same class, i.e. any other class of the same package won’t be able to access it. The keyword used is ‘private’ classes cannot be defined with this access type.

Example

//Java program to illustrate error while sing class from different package with private modifier
package p1;
class A
  {
    private void display()
    {
                     System.out.println("GeeksforGeeks");
    }
  }

 

//Java program to illustrate error while using class from different package with default modifier
package p2;
import p1.*;
//This class is having default access modifier
class ExampleNew
  {
   public static void main(String args[])
    {
     //accessing class Example from package p1
     Example obj = new Example();
     obj.display();
    }
  }

Output 

error: display() has private access in A

obj.display();

c. Protected Access Modifiers in Java

With this keyword, the Java methods or Java datatypes can be accessed within the same package or subclass of a different package. The keyword used is ‘protected’.

Example

//Java program to illustrate protected modifier
package p1;
//Class A
public class A
  {
    protected void display()
     {
       System.out.println("DataFlair");
     }
  }

 

//Java program to illustrate protected modifier
package p2;
import p1.*; //importing all classes in package p1
//Class B is subclass of A

class B extends A
  {
    public static void main(String args[])
     {
        B obj = new B();
        obj.display();
     }
  }

Output

DataFlair

In this program, we have declared two classes P1 and P2, where class A is in P1 and is made public for it to be accessed in P2.

d. Public Access Modifiers in Java

In public modifier in java, there is no restriction, i.e. Java datatypes or methods can be accessed from anywhere in the program. It has the widest scope amongst all the modifiers. It uses the keyword ‘public’.

Example 

//Java program to illustrate public modifier
package p1;
public class A
{
   public void display()
      {
          System.out.println("DataFlair");
      }
}

Output 

DataFlair

 

package p2;
import p1.*;
class B
  {    
         public static void main(String args[])      
          {         
               A obj = new A;         
               obj.display();    
          }
  }

4. Conclusion

In Access Modifiers in Java, we learned about the java access modifiers, types of java Access modifiers, how they define the scope and their use with the help of different examples.

Top Java books to learn Java.

For reference