O'Reilly logo

Special Edition Using Java™ 2 Enterprise Edition by Mark Wutka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required