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
Basis | Private | Public | Protected | No-Modifier (Default) |
---|---|---|---|---|
Subclass accessibility from same package | Private members of a class cannot be accessed by a subclass | Public members of a class can be accessed by a subclass | Protected members of a class can be accessed by a subclass | Members of a class can be accessed by a subclass |
Class accessibility from same package | Private members of a class cannot be accessed by others | Public members of a class can be accessed by others | Protected members of a class can be accessed by others | Members of a class can be accessed by others |
Subclass accessibility from different package | Private members of a class cannot be accessible to a subclass | Public members of a class can be accessible to a subclass | Protected members of a class can be accessible to a subclass | Default members of a class cannot be accessible to a subclass |
Class accessibility from different package | Private members of a class cannot be accessed by others | Public members of a class can be accessed by others | Protected members of a class cannot be accessed by others | Members 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 control | The modifier is applicable for top-level or class-level access control | The modifier is not applicable for top-level or class-level access control | The modifier is applicable for top-level or class-level access control |
Visibility scope | Within a class | Throughout inside and outside packages | Classes within same packages and subclasses from different packages | within same packages |
Accessibility | Least | Highest | Higher than Default modifier | Higher 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:
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:
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: