codingstreets
Search
Close this search box.

Introduction to Access Specifier in Java

woman working at home using her laptop
Photo by Vlada Karpovich on Pexels.com

Access Specifier in Java – This article is about Access Specifier in Java that describes the various access specifier with an example and discusses how they are used.

Before moving ahead, let’s know a bit about Java Access Specifier.

Table of Contents

Access specifier

Access specifier in Java applies some sort of restrictions on how we can access classes, methods, or variables. 

Public

A public access specifier in Java allows a class, method, or variable to be accessed from any other class, regardless of package.

For example, consider the following class:

				
					public class MyFirstClass { //defined class
	public int firstVariable; //defined variable
	public void myFirstMethod(){ //defined method
		
	}
}
				
			

In this example, the variable “firstVariable” and the method “myFirstMethod” are both declared as public, so they can be accessed from any other class in the program. For example, another class in the same package or in a different package can access these members using the following code:

				
					MyFirstClass object = new myFirstClass(); //created new object
object.myFirstMethod(); //stored the method into object
int myVariable = object.myVariable; //stored the variable into object
				
			

Note that the class “MyFirstClass” is also public, in order to be accessed by other classes.

Protected

The protected access specifier in Java allows a class, method, or variable to be accessed from the same package or a subclass in a different package.

For example, consider the following class:

				
					package com.example;

public class Parent {
    protected int x;

    protected void printX() {
        System.out.println(x);
    }
}
				
			
				
					package com.example.sub;

import com.example.Parent;

public class Child extends Parent {
    public void printXFromChild() {
        printX();
    }
}
				
			

In this example, the Child class is in a different package than the Parent class, but it is able to access the protected x field and the printX() method because it extends the Parent class.

Also, any class within the same package as the Parent class would also be able to access the protected x field and the printX() method.

Note: protected access level is less restrictive than the package-private (default) level but more restrictive than the public level.

Default(no keyword)

A class, method, or variable can be accessed from within the same package, but not from the outside of the package.

Note: The default access specifier in Java is used when no access modifier is specified for a class or member.

For example, consider the following code:

				
					package com.example;

class DefaultClass {
    int x;

    void printX() {
        System.out.println(x);
    }
}
				
			

In this example, the class DefaultClass has default access, and it is only accessible within the package com.example. Any class outside this package, including subclasses, will not be able to access the x field or the printX() method of the DefaultClass class.

Note: default access level is less restrictive than the protected and public levels.

Private

A class, method, or variable can be accessed from within the same class.

A class or member having private access cannot be accessed or inherited by any other class, even if it is a subclass or in the same package.

For example, consider the following code:

				
					public class MyClass {
    private int x;

    private void printX() {
        System.out.println(x);
    }
}
				
			

In this example, the field x and the method printX() of the MyClass class have private access. They can only be accessed within the MyClass class, and not by any other class, even if it is a subclass or in the same package.

Note: private access level is the most restrictive among all access levels in Java.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Articles