Access Modifiers in Java
In this article we will talk about access modifiers in Java, which are used for setting the access level to classes, fields, methods and constructors.
There are four access modifiers in Java:
public
protected
default (no keyword required)
private
1. Public
The public access modifier has the widest scope among all other access modifiers.
If we declare classes, methods or data members as public they will be accessible from everywhere in the program. This is the least restrictive access modifier.
package com.guner.accessmodifiers;
public class TestPublic{
public static void display()
{
System.out.println("Test");
}
}package com.guner.accessmodifiers.another;
import com.guner.accessmodifiers.*;
public class TestPublicNew
{
public static void main(String args[])
{
TestPublic obj = new TestPublic();
obj.display();
}
}2. Protected
If we declare classes, methods or data members as protected they will be accessible from the same package and from all subclasses of its class even if they are in other packages.
package com.guner.accessmodifiers;
public class TestProtected
{
protected void display()
{
System.out.println("Test");
}
} package com.guner.accessmodifiers.another;
import com.guner.accessmodifiers.*;
// Class TestProtectedNew is subclass of TestProtected
class TestProtectedNew extends TestProtected
{
public static void main(String args[])
{
TestProtectedNew obj = new TestProtectedNew();
obj.display();
}
} 3. Default
If we don’t declare any access level for classes, methods or data members they will set as default and they will accessible within the same package. They won’t accessible from other packages. The default access modifier is also called package-private.
package com.guner.accessmodifiers;
class TestDefault
{
static void display()
{
System.out.println("Test");
}
}package com.guner.accessmodifiers.another;
import com.guner.accessmodifiers.*;
class TestDefaultNew
{
public static void main(String args[])
{
TestDefault obj = new TestDefault(); //Compile time error
obj.display(); //Compile time error
}
}Output:
Compile time errorIn the above example, the scope of class TestDefault and its method display() is default so it cannot be accessed from outside the package.
Get Utku Güner’s stories in your inbox
Join Medium for free to get updates from this writer.
The default modifier is more restrictive than protected.
4. Private
The private access modifier has the narrowest scope among all other access modifiers. If we declare methods or data members as private they will be accessible from the same class in which they are declared only. This is the most restrictive access modifier.
package com.guner.accessmodifiers;
public class TestPrivate
{
private void display()
{
System.out.println("Test");
}
private void displayAnother()
{
display(); // Available in the same class only
}
}Differences Between Default and Protected
“Default” and “protected” are two commonly confused concepts in object-oriented programming. Here’s the difference between them:
The Protected access modifier is accessible within the same package and also accessible in the subclass whereas the Default is a package level access modifier and it can be accessible in the same package.
- Protected members can be accessible from outside the package only with respect to subclass objects.
- The Default members can be accessible to anything within the same package and can not be accessible outside the package under any condition.
Resources:
- https://www.javatpoint.com/access-modifiers
- https://www.geeksforgeeks.org/access-modifiers-java/
- https://www.baeldung.com/java-access-modifiers
- https://www.tutorialspoint.com/what-are-the-differences-between-protected-and-default-access-specifiers-in-java
- https://selenium-vinod.blogspot.com/p/java-access-modifiers.html




