Difference between public, private, protected and default in Java

For access control, Java provides four access modifiers private, public, protected, and default. The main difference between all these access modifiers can be viewed when we compare the visibility or accessibility scope of a class and its members .

An access modifier defines how a class and its members can access, and basically, controls accessibility. It determines the visibility scope of a class and its member.

Difference between public,private protected and default in Java in Tabular form

BasisPrivatePublicProtectedNo-Modifier (Default)
Subclass accessibility from same packagePrivate members of a class cannot be accessed by a subclassPublic members of a class can be accessed by a subclassProtected members of a class can be accessed by a subclassMembers of a class can be accessed by a subclass
Class accessibility from same packagePrivate members of a class cannot be accessed by othersPublic members of a class can be accessed by othersProtected members of a class can be accessed by othersMembers of a class can be accessed by others
Subclass accessibility from different packagePrivate members of a class cannot be accessible to a subclassPublic members of a class can be accessible to a subclassProtected members of a class can be accessible to a subclassDefault members of a class cannot be accessible to a subclass
Class accessibility from different packagePrivate members of a class cannot be accessed by othersPublic members of a class can be accessed by othersProtected members of a class cannot be accessed by othersMembers of a class cannot be accessed by others
Top level access control ( Class level)The modifier is not allowed for top-level or class-level access controlThe modifier is applicable for top-level or class-level access controlThe modifier is not applicable for top-level or class-level access controlThe modifier is applicable for top-level or class-level access control
Visibility scopeWithin a classThroughout inside and outside packagesClasses within same packages and subclasses from different packageswithin same packages
AccessibilityLeastHighestHigher than Default modifierHigher than Private Modifier

 

Java Private Access Modifier

The ‘private’ access modifier is a member-level access modifier and has the lowest accessibility scope. It specifies that if a member of a class is declared as private, then it would be accessible within the class only. It does not apply to class level and its scope is limited to the class in which it is declared.

Java program to demonstrate accessibility of private access modifier

class A {

private void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // compile time error : getMessage() has private access in A

}
}

Within same package and different package same outcome:

1

Java Protected Access Modifier

The ‘protected’ access modifier provides greater accessibility as compared to default access modifier. It allows a subclass to access members of its parent class from the same package or different packages, but in the case of, a non-subclass, it limits only to the same package.

It is member-level access control, meaning, it is not applicable to a class. This modifier is mostly used in the context of inheritance where the parent-child relationship is maintained.

Java program to demonstrate accessibility of protected access modifier

class A {

protected void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible within same packages

}
}

Try to access non-subclass in different package:

2

Java Public Access Modifier

The ‘public’ access modifier is a top-level access control modifier as it is applicable on both class level and member level. It provides the greatest accessibility as compared to all the modifiers.

A class or method having this access modifier can be accessed inside or outside the package by other classes.

Java program to demonstrate accessibility of public access modifier

class A {

public void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible throughout

}
}

Java Package-Private or Default Access Modifier

A default access modifier is also known as package-private as this signifies a class or its member accessible within a package only, not outside the package. It doesn’t have a specific keyword.

Java treats a class or its member as default if no access modifier is provided explicitly. It applies to both the class level and member level.

Java program to demonstrate accessibility of Default access modifier

class A {

void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible within same package

}
}


Try to access different package:

3

Leave a Reply