Skip to content
Related Articles

Related Articles

Improve Article
Package vs Private Access Modifiers in Java
  • Last Updated : 29 May, 2021

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




// Java Program to illustrate Package Level Access Modifier
   
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
   
// Main Class
class GFG {
   
    // Declaring default variables that is
    // having no access modifier
    String s = "Geeksfor";
    String s1 = "Geeks";
   
    // Method 1
    // To declare a dafault method
    String fullName()
    {
        // Concatenation of strings
        return s + s1;
    }
   
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
   
        // Creating an object of main class(GFG)
        // in the main() method
        GFG g = new GFG();
   
        // Calling method1 using class instance
        // and printing the concation of strings
        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




// Java Program to showcase Private Access Modifier
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Class 1
// Helper class
class A {
 
    // Method
    private void m1()
    {
 
        // Print statement whenever the method is called
        System.out.println("GFG");
    }
}
 
// Class 2
// Main class
class B {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of type class A
        // in main() method
        A a = new A();
 
        // Now accessing the method m1() of
        // class created above
        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 ModifierPrivate 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.

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :