"The encapsulation can be easily broken, because external code can define classes in the same packages used by your code and thus get access to your package-private declarations."
I am not able to understand how can we achieve what is written in the statement. How can encapsulation be broken?
You can see what this means with a small example.
Here we have a class with some
package-privatevisibility variables, the visibility applied when not using a visibility keyword.Here I have a class in another package. This will throw none visibility errors for the variables.
Here I have a class in the same package as our
package-privatevariables class. This one does not throw an error when accessing the variables.File Structure Overview: