Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected access modifier in Java

I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that inherit the class containing an abstract member.

I wrote the following sample code. I see that the commented out line produces a compilation error if uncommented. Why can I access pro through a Second object in Second but not through a First object in Second?

package first;

public class First {

    protected void pro(){
        System.out.println("Can see protected method");
    }

}

package first;

public class InFirst {


    public static void main(String[] args){
        First fst = new First();
        fst.pro();
    }

}

package second;

import first.First;

public class Second extends First {

    public static void main(String[] args){

        First fst = new First();

//      fst.pro();

        Second sec = new Second();
        sec.pro();

    }
}
like image 516
B M Avatar asked Apr 11 '13 00:04

B M


People also ask

What is Access Protection in Java?

protected: The protected access modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages.

What is an protected access specifier?

Remarks. The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.

Why protected access modifier is used?

If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that's why we have used protected modifier.

Where is protected access modifier used?

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. It provides more accessibility than the default modifer.

What is the scope of protected access modifier in Java?

What is the scope of protected access modifier in Java? When a variable, method or constructor that are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

What are access modifiers in Java?

The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class. There are 4 types of java access modifiers: private. default. protected. public.

What is protected in Java?

Protected is one of the trickier of the Java access modifiers, but it is not difficult to understand! Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them. Let's say we define a protected method in Animal called eat ().

What is the protected access modifier in Python?

protected: The protected access modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages. In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2.


2 Answers

The webpage @MadProgrammer linked gives a decent explanation:

"The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package."

This means the protected member must be accessed directly through either the class it is defined in or a subclass of said class while also being within the appropriate package. It does not necessarily mean you can access the protected member through an instance of said class created within a subclass of said class. The focus is on the packages involved.

Here are your examples:

package first; // Current package

First fst = new First(); // from package first and does not extend anything
fst.pro();

Attempting to access member in question from which package? first

Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, First is defined in package first, so the protected member is accessible from First in package first.

package second; // Current package

First fst = new First(); // from package first and does not extend anything
fst.pro();

Attempting to access member in question from which package? second

Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? No, First is defined in package first, so protected makes the member inaccessible from First in package second.

package second; // Current package

Second sec = new Second(); // from package second and extends First from package first
sec.pro();

Attempting to access member in question from which package? second

Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second, which is defined in package second, inherits the member from First, so the protected member is accessible from Second in package second.

More examples for clarity:

package first; // Current package

Second sec = new Second(); // from package second and extends First from package first
sec.pro();

Attempting to access member in question from which package? first

Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Second inherits the member from First, which is defined in package first, so the protected member is accessible from Second in package first.

package first; // Current package

Third third = new Third(); // from package third and extends Second from package second,
                           // which extends First from package first
third.pro();

Attempting to access member in question from which package? first

Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes, Third inherits the member from Second, which inherits it from First where the member is defined (package first), so the protected member is accessible from Third in package first.

like image 130
Ryan Avatar answered Oct 19 '22 11:10

Ryan


protected vs. defaultHere is a diagram to show the access level. You code belongs to the R(Reference) case in Convertible in the diagram. That is, reference in subclass in another package is not allowed to access.

like image 36
Ryan Avatar answered Oct 19 '22 11:10

Ryan