Access Modifiers in Java – Enhance Your Programming Skills

Access Modifiers in Java are used to define access to various classes and objects, functions, and constructors. They are also used to specify the scope of any method, class etc.

This tutorial is specially designed to learn different types of Java Access Modifiers with examples. So, what are you waiting for?

Types of Access Modifiers in Java

Let’s discuss different types of access modifiers in Java with examples.

Access Modifiers in Java

Before we start the tutorial, you should know what are Methods in Java?

1. Default

The default access modifiers in Java are used to define access when no other modifier is specified for the class or function, they are accessible only within the same package.

Example-

//Java program to illustrate default modifier
package JavaAcessModifier;
//Class Example is having Default access modifier
public class DefaultAcessModifier {
  void display()
    {
    System.out.println("Welcome to the DataFlair's Tutorial of Java!");

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

Output-

Compile-time error

Brush up your Fundamental Skills with Primitive and Non-Primitive Data Types in Java

2. Private

The keyword private is used to define access to a 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.

Example-

//Java program to illustrate error while sing class from different package with private modifier
package JavaAcessModifier;

public class PrivateAcessModifier {
private void display()
      {
        System.out.println("Welcome to the DataFlair's Tutorial of Java!");
      }
//Java program to illustrate error while using class from different package with default modifier
package JavaAcessModifier1;
import JavaAcessModifier.*;
//This class is having default access modifier
public class PrivateAcessModifier1 {
  
public static void main(String args[])
      {
       //accessing class Example from package p1
  PrivateAcessModifier1 obj = new PrivateAcessModifier1();
       obj.display();
      }
    }

Output- 

error: display() has private access in PrivateAcessModifier
obj.display();

Join DataFlair on Telegram

3. Protected

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 JavaAcessModifier;
import JavaAcessModifier1.*;
public class ProtectedAcessModifier {
 protected void display()
       {
         System.out.println("Welcome to the DataFlair's Tutorial of Java!");
       }
    }
//Java program to illustrate protected modifier
package JavaAcessModifier1;
import JavaAcessModifier.*;//importing all classes in package JavaAcessModifier
//Class ParentClass is subclass of ChildClass

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

Output-

Protected Acess Specifier

Here, we declare two packages JavaAcessModifier and JavaAcessModifier1, where class ProtectedAcessModifier is in JavaAcessModifier package and made public for it to access in JavaAcessModifier1 package.

Do you know how to initialize Array in Java?

4. Public

In public access modifier in Java, there is no restriction, i.e. 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-

package JavaAcessModifier;
//import JavaAcessModifier1.*;
public class PublicAcessModifier {
  public void display()
             {
           System.out.println("Welcome to the DataFlair's Tutorial of Java!");
          }
  }

package JavaAcessModifier1;
import JavaAcessModifier.*;

public class PublicAcessModifier1 {
 public static void main(String args[]) 
        {
   PublicAcessModifier obj = new PublicAcessModifier();
                 obj.display();
      }
}

Output-

Public Acess Specifier

Summary

Access modifiers in Java are used to define the accessibility of class members. These modifiers place some restrictions to the class members to not to get directly accessed by outside functions.

DataFlair offers you 140+ free Java tutorials to master the language.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.