In Java, there are four types of access modifiers. These are public, private, default, and protected. To get the idea of these modifiers, you can refer to access modifiers in java. In this article, we discuss the accessibility of protected members in different cases.
Now let us discuss various scenarios of accessing protected members which are listed below as follows:
- Accessing in the same class
- Accessing in other classes of the same package
- Accessing protected members of a class in its subclass in the same package
- Accessing another class in a different package
- Accessing in sub-class in a different package
Case 1: Accessing protected members in the same class
We can access protected members of a class anywhere in it.
Example:
Java
// Java Program to Illustrate// Accessing Protected Members// in the same classÂ
// Main class class Sample {       protected int year = 2021;    protected void printYear()    {        System.out.println("Its " + year + " !!");    }Â
    public static void main(String[] args)    {        Sample sample = new Sample();        System.out.println(sample.year);        sample.printYear();    }} |
2021 Its 2021 !!
Case 2: Accessing protected members in other classes of the same packageÂ
We can access protected members of a class in another class that is present in the same package.
Java
// Java Program to Illustrate Accessing// Protected Members// In Other Class of Same PackageÂ
// Class 1class Sample {Â Â Â Â protected int year = 2021;Â Â Â Â protected void printYear() {Â Â Â Â Â Â Â Â System.out.println("Its "+year+" !!");Â Â Â Â }}Â
// Class 2public class Test {       // Main driver method    public static void main(String[] args) {        Sample sample = new Sample();        System.out.println(sample.year);        sample.printYear();    }} |
2021 Its 2021 !!
Case 3: Accessing protected members of a class in its subclass in the same packageÂ
We can access protected members of a class in its subclass if both are present in the same package.
ExampleÂ
Java
// Java Program to Illustrate// Accessing Protected Members// of a class in its subclass// in the same packageÂ
// Class 1class Sample {Â Â Â Â static protected String title = "geekforgeeks";Â Â Â Â protected int year = 2021;Â Â Â Â protected void printYear() {Â Â Â Â Â Â Â Â System.out.println("Its "+year+" !!");Â Â Â Â }}Â
// Class 2public class Test extends Sample {Â Â Â Â public static void main(String[] args) {Â Â Â Â Â Â Â Â Sample sample = new Sample();Â Â Â Â Â Â Â Â System.out.println(sample.year);Â Â Â Â Â Â Â Â sample.printYear();Â Â Â Â Â Â Â Â System.out.println(Sample.title);Â Â Â Â }} |
2021 Its 2021 !! geekforgeeks
Case 4: Accessing protected members in another class in a different packageÂ
We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.Â
Example 1: Package 1Â
Java
// Java Program to Illustrate// Accessing Protected Members// In Another Class in a// Different PackageÂ
// Package 1package package1;Â
// Main classpublic class Sample {       static protected String title = "neveropen";    protected int year = 2021;       // Protected method    protected void printYear() {        System.out.println("Its "+year+" !!");    }} |
Â
Example 2: Package 2Â
Java
// Java Program to Illustrate// Accessing Protected Members// In Another Class in a// Different PackageÂ
// Package 1package package2;// Importing above packageimport package1.Sample;Â
// Main classpublic class Test {       // Main driver method    public static void main(String[] args) {               Sample sample = new Sample();        System.out.println(sample.year);        sample.printYear();        System.out.println(Sample.title);    }} |
Â
Output:Â Â
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: title has protected access in Sample
System.out.println(Sample.title);
^
It will give a compile-time error. In the following example, we will create two classes. Sample class in package1 and Test class in package2 and try to access protected members of Sample class in Test class. It is justified in the above two examples.
Case 5: Accessing protected members in sub-class in a different package
We can access protected members of a class in its subclass present in a different package. In the following example, we will create two classes. Sample class in package1 and Child class in package2. Child class extends Sample class.
Example 1.1Â
Java
// Java Program to Illustrate Accessing Protected// Members in sub-class in a different packageÂ
package package1;Â
// Classpublic class Sample {Â
    // Protected attributes    static protected String title = "neveropen";    protected int year = 2021;    protected void printYear()    {        System.out.println("Its " + year + " !!");    }} |
Â
Example 1.2Â
Java
// Java Program to Illustrate Accessing Protected// Members in Sub-class in a different Packagepackage package2;// Importing class from above packageimport package1.Sample;Â
// Main classpublic class Child extends Sample {Â
    // Method 1    void helper()    {        System.out.println(year);        printYear();        System.out.println(Sample.title);    }Â
    // Method 2    // Main driver method    public static void main(String[] args)    {Â
        // Creating child class instance inside main()        Child child = new Child();        child.helper();    }} |
OutputÂ
2021 Its 2021 !! neveropen
Note: From the above output it can be perceived we have successfully accessed the protected members directly as these are inherited by the Child class and can be accessed without using any reference. The protected members are inherited by the child classes and can access them as its own members. But we can’t access these members using the reference of the parent class. We can access protected members only by using child class reference.Â
Example 2Â
Java
// Java Program to Illustrate Compile-time Error Thrown// When we are Trying to Access Protected Members// Using Parent Class ReferenceÂ
// Importing packagespackage package2;import package1.Sample;Â
// Importing class extending to Parent classpublic class Child extends Sample {Â
    // Method 1    void helper()    {        // Creating instance of Child class inside main()        Child myself = new Child();Â
        // As Child is sub-class of Sample, we can access        // the static variable here        System.out.println(Sample.title);Â
        // This will compile fine as we are accessing year        // using child class reference variable        System.out.println(year);        System.out.println(myself.year);Â
        // This will compile fine as we are accessing        // printYear() method using child class reference        // variable        printYear();        myself.printYear();Â
        // Creating parent class object        Sample sample = new Sample();Â
        // Parent class reference holding child class object        Sample child = new Child();Â
        // Note: Below lines of code won't compile as we are        // trying to access protected members of parent        // class using Parent's class reference which is        // present in other packageÂ
        // Errors will be thrown        System.out.println(sample.year);        sample.printYear();        child.printYear();    }Â
    // Method 2    // Main driver method    public static void main(String[] args)    {        // Creating child class instance inside main()        Child child = new Child();Â
        // Calling the above method 1 of child class        child.helper();    }} |
OutputÂ
error: year has protected access in Sample
System.out.println(sample.year);
^
error: printYear() has protected access in Sample
sample.printYear();
^
error: printYear() has protected access in Sample
child.printYear();
^
So the main difference between default access modifiers and the protected modifier is that default members are accessible only in the current package. While protected members can be accessed anywhere in the same package and outside package only in its child class and using the child class’s reference variable only, not on the reference variable of the parent class. We can’t access protected members using the parent class’s reference.
Â
