Does reflection breaks the idea of private methods, because private methods can be access outside of the class?

前端 未结 14 1103
甜味超标
甜味超标 2020-11-28 08:06

Does reflection break the idea of private methods? Because private methods can be accessed from outside of the class? (Maybe I don\'t understand the meaning of reflection or

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 08:32

    Yes it breaks the encapsulation, if you want it to. However, it can be put to good use - like writing unit tests for private methods, or sometimes - as I have learned from my own experience - getting around bugs in third party APIs :)

    Note that encapsulation != security. Encapsulation is an object oriented design concept and is only meant for improving the design. For security, there is SecurityManager in java.

提交回复
热议问题