HomeJavaAccess Modifiers in Java

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

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022