Access Modifiers in Java

The access modifiers in Java is used to specify the accessibility or scope of a field, method, constructors, classes, variables, interfaces, methods, data member and setter methods. We can also limit the level of accessibility of them.

Basically there are four different types of access modifiers in Java. They are:

  • Private
  • Default
  • Protected
  • Public.

Let us further understand the level of access modifiers in Java.

Access ModifierWithin classWithin packageOutside package by subclass onlyOutside package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes

Let’s see the access modifiers in detail

Private access modifier

  • Firstly, the private access modifier in Java is accessible only within the class, i.e. Methods, variables, and constructors which are declared private can only be accessed only within the declared class.
  • The private access modifier is denoted using the keyword private.
  • Higher-level classes or interfaces cannot be declared as private because private means “only visible within the declared class”.
  • Any other class of the same package will not be able to access the members of the private class.

Sample program for private access modifier in Java programming

package firstPackage; 
 
class A 
{ 
private void display() 
    { 
        System.out.println("Printing first package"); 
    } 
} 
 
class B 
{ 
public static void main(String args[]) 
    { 
        A obj = new A(); 
         obj.display(); 
    } 
}

Output:

display() has private access in A
obj.display()

Default Access Modifier

  • There is no Keyword to specify the default modifier.
  • Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. which means it is available to any other class in the same package by default.

Sample program for Default access modifier in Java programming

class A 
{ 
void display() 
    { 
        System.out.println("Printing first package"); 
    } 
} 
 
public class Main 
{ 
    public static void main(String args[]) 
    { 
        A obj = new A(); 
         obj.display(); 
    } 
}

Output:

Printing first package

Protected access modifier

The protected access modifier is denoted using the keyword protected.

  • The methods or data members declared as protected are accessible within the same package or subclasses in different packages.
  • Variables, methods, and constructors, which are declared protected in a superclass, can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.
  • The protected access modifier is not being applied to class and interfaces. Methods, fields can be declared protected, but methods and fields in an interface cannot be declared ‘protected’.

Sample program for Protected access modifier in Java programming

class Super {
    protected void display() {
        System.out.println("I am inside the protected modifier");
    }
}

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

Output

I am inside the protected modifier

Public access modifier

The public access modifier is denoted using the keyword public

  • The public access modifier has the widest scope among all other access modifiers. Classes, methods, or data members etc, which are declared as public, are accessible from anywhere in the program. There is no restriction on the accessibility of public data members.
  • However, if the public class which is about to be accessed is in a different package, then the public class needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

Sample program for public access modifier in Java programming

package firstPackage; 
public class A 
{ 
public void display() 
    { 
        System.out.println("I am in public access modifier"); 
    } 
} 
package secondPackage; 
import p1.*; 
class B 
{ 
    public static void main(String args[]) 
    { 
        A obj = new A; 
        obj.display(); 
    } 
}

Output

I am in public access modifier

Leave A Reply

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

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024