-1

Why do protected attributes in a Java class can be accessed by the other classes of the same package ?

I thought it was only accessible through inheritance.

A.java

package abc;
class A {
    protected int attr1;
}

B.java

package abc;
class B {
    B() {
        A obj = new A();
        obj.attr1 = 2;
    }

    public static void main(String[] args) {
        B obj2 = new B();
    }
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
Libert Piou Piou
  • 540
  • 5
  • 22
  • What you're asking is why they have the `private` visibility modifier. `protected` is for classes in the same package. – Drew Kennedy Jan 07 '15 at 19:28
  • 1
    @DrewKennedy No. The default visibility (i.e. no `private`, no `public`, no `protected`) is package-private. `protected` means (AFAIK) only subclasses have access to it. It is not package-private. – Nic Jan 07 '15 at 19:43
  • @newbiedoodle Protected doesn't mean only subclasses have access to it. – peter.petrov Jan 07 '15 at 19:47
  • @peter.petrov Yeah, just looked it up. It's just that no class outside the package can get it. – Nic Jan 07 '15 at 19:48
  • @newbiedoodle uhh, nowhere was the default visibility modifier mentioned, but thanks? – Drew Kennedy Jan 07 '15 at 19:52
  • @DrewKennedy I was confused, and thought you were saying that it was *only* visible to the same package, which is a pretty easy conclusion to draw from your comment. That's otherwise known as the 'default visibility', since you get it by not putting any keyword in front of the class. – Nic Jan 07 '15 at 19:54
  • @newbiedoodle no worries. I was just surprised about your comment haha. I was just saying that methods or fields that have the `protected` modifier are only available to classes of the package they are contained in. :) – Drew Kennedy Jan 07 '15 at 19:58
  • 1
    @DrewKennedy They are also available to any subclasses of the class you're defining. – Nic Jan 07 '15 at 20:01
  • The reason is most likely that if it worked that way, and you wanted to allow access to the package *and* to subclasses, you would have to resort to `public`, in which case encapsulation would suffer. The way it has been designed makes the access modifiers increasingly wider, as shown clearly in [this chart](http://stackoverflow.com/a/33627846/276052). – aioobe Jun 10 '16 at 17:49

2 Answers2

1

Because that was the decision of the language creators.

Methods/fields with the protected access modifier are accessible
by both classes from the same package as the class defining them,
as well as by subclasses of the class defining them. And of course,
they are also accessible by the defining class itself.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

You should think about it this way: protected is a kind of public access level. Protected members are a part of public API in classes designed for extension. It would make no sense for a member accessible to the API client to be inaccessible to the API implementation, especially at a place where another member with default access level (which is non-public) is accessible.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436