Skip to content
Related Articles

Related Articles

Protected vs Package Access Modifiers in Java
  • Last Updated : 28 Mar, 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.

Modifier 1: Protected Access Modifier

This modifier can be applied to the data member, method, and constructor, but this modifier can’t be applied to the top-level classes and interface. A member is declared as protected as we can access that member only within the current package but only in the child class of the outside package.

Example:

Java




// Java Program to show Protected keyword
  
// Importing input output classses
import java.io.*;
// Importing utility classes
import java.util.*;
  
// Class 1
// Parent class
class Parent {
  
    // declaring a protected method m1()
    protected void print() { System.out.println("GFG"); }
}
  
// Class 2
// Child class which is extending Parent class
class Child extends Parent {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of parent class
        // using parent reference
        Parent p = new Parent();
  
        /// calling the print() method of Parent class
        p.print();
  
        // Creating an object of child class
        // using child reference
        Child c = new Child();
  
        // Calling the print() method of Parent class
        c.print();
  
        // Creating an object of child class
        // using parent reference
        Parent cp = new Child();
  
        // Calling the print method over this object
        cp.print();
    }
}
Output



GFG
GFG
GFG

Output explanation:

In the above example, we create three objects using parent reference and child reference and call m1() method on it, and it successfully executed so from the above example we can say that we can access the protected method within the current package anywhere either by using parent reference or by child reference.

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 packagelevel 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 {
  
    // Declarig default varibales that is
    // having no access modifier
    String s = "Geeksfor";
    String s1 = "Geeks";
  
    // Method 1
    // To declare a dafault method
    String fullName()
    {
  
        // Concatination 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

Finally, after getting it done with both o them let us conclude the evident differences between them

                 Package access modifier                                  Protected access modifier 
This modifier is applicable to both top-level classes and interfaceThis modifier is not applicable to both top-level classes and interface
We cannot access this modifier from the child class of the outside package.We can access this modifier from the child class of the outside package, but we should use child reference only. 
This modifier is more restricted than the protected modifier.This modifier is more accessible than the package level modifier.

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 :