Access Modifiers in Java – Types and Examples

1. Objective

In our last tutorial, we talked about Java Operators. Today, we will open a new chapter called Access Modifiers in Java. They are used for restricting the scope of any Java class, object, function, or a constructor. Along with this, we will discuss types of Java Modifiers.

So, let us start Access Modifiers in Java.

Access Modifiers In java

Access Modifiers in Java – Types and Examples

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

Do you know What is Constructor in Java?

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.
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 Modifiers in Java 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 define 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 Java keyword, the methods or datatypes can access 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

Do you know What is Java Closure and Java 8 Lambda Expressions

Here, we declare two classes P1 and P2, where class A is in P1 and made public for it to access in P2.

d. Public Access Modifiers in Java

In public modifier in java, there is no restriction, i.e. Java datatypes or methods can access 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();    
          }
  }

So, this was all about Access Modifiers in Java. Hope you like our explanation.

4. Conclusion

Hence, we learned what are access modifiers in Java and their types. In addition, we discussed how they define the scope and their use with the help of different examples. Furthermore, if you have any query, feel free to ask in the comment section.

See Also – String vs StringBuffer vs StringBuilder in Java

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *