Suppressing Access Restrictions
There are times when your Reflection code must access private and protected members of another class. Although that sounds like a violation of good object-oriented principles, it's actually a reasonable thing to do when you're writing a library that does data mapping (such as serialization or object-to-relational mapping).
The reason you normally create access restrictions is to keep other classes from becoming dependent on parts of your class that are implementation specific. For example, if you use a counter to keep track of the number of items in an order, you're better off creating a getItemCount method rather than exposing the counter to any client classes. Otherwise, you'd be obligated to keep the counter, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access