Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access modifiers. So access modifiers are used to set accessibility of classes, methods, and other members.
Lets us discuss both of the modifiers in-depth individually
- Package(Default) Access Modifier
- Private Access Modifier
Modifier 1: Package(Default) Access Modifier
A class or method or variable declare without any access modifier then is considered that it has a package(default)access modifier The default modifier act as public within the same package and acts as private outside the package. If a class is declared as default then we can access that class only within the current package i.e from the outside package we can’t access it. Hence, the default access modifier is also known as the package–level access modifier. A similar rule also applies for variables and methods in java.
Example:
Java
import java.io.*;
import java.util.*;
class GFG {
String s = "Geeksfor" ;
String s1 = "Geeks" ;
String fullName()
{
return s + s1;
}
public static void main(String[] args)
{
GFG g = new GFG();
System.out.println(g.fullName());
}
}
|
Output:
GeeksforGeeks
Modifier 2: Private Access Modifier
This modifier is not applicable for top-level classes or interfaces. It is only applicable to constructors, methods, and fields inside the classes. If a variable or methods or constructor is declared as private as we can access them only from within the class i.e from outside the class we can’t access them.
Example:
Java
import java.io.*;
import java.util.*;
class A {
private void m1()
{
System.out.println( "GFG" );
}
}
class B {
public static void main(String[] args)
{
A a = new A();
a.m1();
}
}
|
Output:

Finally, after getting it done with both of them let us conclude the evident differences between them which are as follows:
Package Access Modifier |
Private Access Modifier |
This modifier is applicable for both top-level classes and interfaces. |
This modifier is not applicable for both top-level classes and interfaces. |
Package members can be accessed from the child class of the same package. |
Private members cannot be accessed from the child class of the same package. |
Package members can be accessed from a non-child class of the same package. |
Private members cannot be accessed from a non-child class of the same package. |
The package modifier is more accessible than the private modifier. |
The private modifier is more restricted than a package modifier. |
Package modifier provides the lowest level of Encapsulation in java. |
A private modifier provides a higher level of Encapsulation in Java. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Mar, 2022
Like Article
Save Article