0

I use the protected modifier in the following code but it is not working as I would expect.

This is my Prob3.java file and I expected to have error when compiling ob1.x=4; Can anyone explain why I'm not getting one?

class Coordinates2D{
    protected int x,y;
    public Coordinates2D(int x,int y){
        this.x=x;
        this.y=y;
    }
    public int getX(){
        return this.x;  
    }
    public int getY(){
        return this.y;
    }
    public void setX(int val){
        this.x=val;
    }
}

public class Prob3{
    public static void main(String[] args){
        Coordinates2D ob1 = new Coordinates2D(3,4);
        ob1.x=4;  
        System.out.println("Atributele ob 2D: "+ ob1.getX()+" , "+ob1.getY());
    }
}
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
  • Possible duplicate: http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private `protected` allows access in the same package. – clcto Nov 14 '14 at 18:35
  • You're trying to set the object value outside of the class Coordinates2D. Because ob1.x is protected it's going to give you an error. – CBC_NS Nov 14 '14 at 18:35
  • 3
    Are they in the same package? If they are, then this works fine. protected means it can be viewed within a package. If you only want it viewed by that class, you should use private. – AdamMc331 Nov 14 '14 at 18:35
  • 1
    Exactly I expected to have a compile error but it works. And normally if I use ob1.x=4 in MAIN it should`n work too – Taut Adrian Nov 14 '14 at 18:37
  • 1
    Are both of these classes in the same package, though? – AdamMc331 Nov 14 '14 at 18:38
  • I suspect that they are, if this is all one file. – AdamMc331 Nov 14 '14 at 18:39
  • Yes are in the same Package – Taut Adrian Nov 14 '14 at 18:40
  • That's the problem. I linked to resources in my answer, if you still don't understand. – AdamMc331 Nov 14 '14 at 18:43

3 Answers3

1

The protected keyword limits the scope of the accessibility of either a variable or a function to within the same package, or any subclass inheriting the class with said variable or function. This means that as long as two classes are in the same package, they will be able to access each other's attributes and methods with the protected keyword. In this case they're not only in the same package, but also in the same class file. If you wanted to restrict the access of x,y to only within the Coordinates2D class, use the keyword private instead.

Kylorcache
  • 31
  • 3
  • 1
    Just to make it super clear: both of your classes are in the same package if they are defined within the same .java file - hence protected works as expected in your scenario. – JBA Nov 14 '14 at 18:42
0

protected members are visible to

  • the class itself
  • any subclasses of the class, and
  • any classes in the same package.

Since these classes are in the same file, they're in the same package, and therefore they will have access.

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
0

The reason you are not getting an error is because you're not doing anything wrong. However, you think you are doing something wrong so let me explain:

The protected keyword allows for a variable to be viewed in any classes in the same package.

Since your prob3 class and your Coordinates2D class are in the same package, protected variables can be accessed in both. If you want to limit the availability of the x and y variables, you should set their access modifier to private:

private int x, y;

Try it, and leave the rest of your code the same. You will get the compiler error you expect.

Community
  • 1
  • 1
AdamMc331
  • 16,492
  • 10
  • 71
  • 133