Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access a protected variable in Java this way?

Tags:

java

protected

I've a class defined this way:

package prueba;

public class OtraClase {

[...]

protected int num3;

[...]

And another class defined this way:

package otro;

import prueba.*;

public class OtraClaseMas extends OtraClase{

But if in that last class I create an OtraClase object I cannot do something like this:

createdObjectOfOtraClase.num3=1;

And I think that according to the documentation I should be able to, here. It says that the protected modifier allows for access by a subclass of its class in another package. And as much as I look at it I don't see it being another thing than exactly a subclass of its class in another package.

Am I misunderstanding something?

Edit: I'm either using the constructor of the class and in another different function and it doesn't work in neither place.

Code for the constructor:

public OtraClaseMas(int num, int num2,int num3)
{
    super(num, num2,num3);      
    OtraClase oc=new OtraClase(1,1,1);
   //oc.num3=1; This doesn't work
}

Code for the method:

public void foo()
{
    OtraClase oc=new OtraClase(1,1,1);
    //oc.num3=1; This doesn't work

}
like image 274
user2638180 Avatar asked May 21 '16 11:05

user2638180


2 Answers

A protected variable can be accessed from the same package or by the derived classes the same way they access their own members, but if you create an instance of the super class outside of the package of the superclass, you wont be able to access it even if you are extending this same superclass

package packageA;
public class ClassA {
    protected int variableA;
}

package packageA;
public class ClassC {
    public void setVariableA() {
        ClassA classA = new ClassA();
        classA.variableA = 1; // OK
    }
}

package packageB;
public class ClassB extends ClassA {
    public void setVariableA() {
        ClassA classA = new ClassA();
        variableA = 1; // OK
        classA.variableA = 1;// this wont work
    }
}
like image 90
illgoforit Avatar answered Nov 15 '22 04:11

illgoforit


About the Protected class:

The simplest answer is that your base class (SuperClass) that you are extending exists in its own package (let's say in a bedroom in a planet Mars) with its protected field (Let's say a Laser gun).

When you implement this class by inheritance (i.e extending) to a different package and class (let's say an office in planet Earth) you are creating a portal from earth to Mars to the Laser gun in a protected portal that exists in the office only and only this portal can get you the gun.

Therefore an alien bedroom (base Class) on Earth (in the new package) doesn't know and can not refer you to the laser gun because they don't have the portal the office has and therefore you can not ask them to refer you to the laser gun. but you can ask the office and anything that creates a portal to the office for reference.

so now you have a gun and can Pew! pew!

NB: if you are in the same package you don't need a portal (package private / default)

like image 29
MichaelDHaile Avatar answered Nov 15 '22 04:11

MichaelDHaile