Firstly excuse my english, I have one question. Protected modifier expose to all classes in the same package and to all subclasses in different packages. Isn't it useless? when you have it in your package with class with main method it act like public, and if you have it in different package with all subclasses you don't have to use any modifier becauce any modifier expose only to package, am i right?
Asked
Active
Viewed 46 times
0
-
You confused `protected` with friendly access. – Nikolas Charalambidis Aug 05 '18 at 20:02
-
`protected` gives access in the class itself, to everything in the same package and to subclasses. It denies access to classes in different packages that are not subclasses. As such it is not useless, there is no other way of achieving this access combination. Suppose you have a class `Dog extends Animal` and the `Animal` has a member `amountOfLegs`. For some reason you don't want `Dog` to be in `Animal`s package. Then you need `protected` for `Dog` to be able to access the member. Take a look at the [documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). – Zabuzard Aug 05 '18 at 20:04