1

I'm a bit confused about the whole protected thing in java. If something is protected only the classes within the same package can access it, right?

Should we use protected private attributes in a class? And if so, when?

2

2 Answers 2

1

protected members are accessible by other classes in the same package and by classes extending the class with that member, regardless of its package.

private members are accessible only from within the class. There is no such thing as a "protected private attribute".

1
  • Oh thank you! I got it now Commented Dec 2, 2017 at 18:20
0

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.

Source: Controlling Access of Members of a Class

For the discussion about "protected private", see this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.