Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are protected instance members not visible inside a subclass within a different package, but protected class members are? [duplicate]

Tags:

java

package one;

public class A {
    protected int first;
    protected static int second;
}

package two;

import one.A;

public class B extends A {
    public void someMethod() {
        this.first = 5; //works as expected
        B.second = 6; //works
        A a = new A();
        // a.first = 7; does not compile

        //works just fine, but why?
        a.second = 8; 
        A.second = 9;
    }
}

Why aren't the same restrictions applied to static fields, what's the idea behind it?

like image 216
мајдејсаремех Avatar asked Sep 02 '17 17:09

мајдејсаремех


People also ask

Can we access the protected members of class with in different package by non subclass explain in detail?

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.

Can protected methods be accessed by classes in different package?

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.

How do you access protected variables outside the package?

The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only. We can't assign protected to outer class and interface. If you make any constructor protected, you cannot create the instance of that class from outside the package.

Which members can't be accessed in subclasses?

Private members of superclass are not directly accessible to subclass. As in this example, Animal variable noOfLegs is not accessible to Cat class but it can be indirectly accessible via getter and setter methods. Superclass members with default access is accessible to subclass ONLY if they are in same package.


1 Answers

From JLS 6.6.2:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

From 6.6.2.1:

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

this.first = 5; works because B is an implementor of A.

A.second works because this restriction is only defined for members of an object. Same goes for B.second.

As for why it's specified this way, you'd have to ask the people defining the specification - we could only make assumpsions. 6.6.2.1 even has an example that expresses an issue similar to this:

Consider this example, where the points package declares:

package points;
public class Point {
    protected int x, y;
    void warp(threePoint.Point3d a) {
        if (a.z > 0)  // compile-time error: cannot access a.z
            a.delta(this);
    }
}

and the threePoint package declares:

package threePoint;
import points.Point;
public class Point3d extends Point {
    protected int z;
    public void delta(Point p) {
        p.x += this.x;  // compile-time error: cannot access p.x
        p.y += this.y;  // compile-time error: cannot access p.y
    }
    public void delta3d(Point3d q) {
        q.x += this.x;
        q.y += this.y;
        q.z += this.z;
    }
}

A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). The method delta3d can access the protected members of its parameter q, because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d.


I recommend checking out Why we should not use protected static in Java.

The semantics of protected were aimed at instance members - protected static is contradictive to the purpose of protected, which could be why it wasn't restricted in the same manner.

like image 182
Vince Avatar answered Sep 22 '22 21:09

Vince