Open In App

Protected vs Package Access Modifiers in Java

Last Updated : 02 Mar, 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.

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 classes
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 {
 
    // 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 concatenation 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 interface This 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.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads