Open In App

Public vs Package Access Modifiers in Java

Last Updated : 18 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 as follows:

  • Public Access Modifiers
  • Package(Default) Access Modifier

Modifier 1: Public Access Modifiers

If a class is declared as public then we can access that class from anywhere.

We will be creating a package pack1 inside that package we declare a class A which is public and inside that class, we declare a method m1 which is also public. Now we create another package pack2 and inside that pack2 we import pack1 and declare a class B and in class B’s main method we create an object of type class A and trying to access the data of method m1.

Example 1:

Java




// Java Program to illustrate  Public Access Modifiers
 
// creating a package
package pack1;
   
// import required packages
import java.io.*;
import java.util.*;
   
// declaring a public class
public class A {
     
    // declaring method m1
    public void m1() { System.out.println("GFG"); }
}


Output: Compiling and saving the above code by using the below command line:
 

Here we will be importing the above class of the created package to the newly created package. 

Example 2: 

Java




// Java Program to illustrate  Public Access Modifiers
 
// creating a package
package pack2;
 
// importing class from above package
// package pack1
// import required packages
import java.io.*;
import java.util.*;
import pack1.A;
 
// Main class
class B {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of type class A
        A a = new A();
 
        // accessing the method m1()
        a.m1();
    }
}


Output Explanation:

If class A is not public while compiling B class we will get a compile-time error saying pack1. A is not public in pack1 and can’t be accessed from the outside package. Similarly, a member or method, or interface is declared as public as we can access that member from anywhere.

Modifier 2: 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 default 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 concatenaion of strings
        System.out.println(g.fullName());
    }
}


 Output: 

GeeksforGeeks

Finally, after understanding and going through both of them let us conclude the evident differences between them which are depicted in the table below as follows:
 

                  Public Access Modifier                  Package Access Modifier
Public members can be accessed from a non-child class of outside package. This modifier can’t be accessed from a non-child class of the outside package.
Public members can be accessed from child class of outside package. We cannot access this modifier from the child class of the outside package.
The public modifier is more accessible than the package access modifier. This modifier is more restricted than the public access modifier.

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads