Are private methods really safe?

后端 未结 7 1699
旧巷少年郎
旧巷少年郎 2021-01-30 15:26

In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn\'t know about that method either.

7条回答
  •  别跟我提以往
    2021-01-30 16:14

    Assuming you trust the client programmer of your API, another way of looking at is how 'safe' it is for them to use those particular functions.

    Your publicly available functions should provide a clear, well-documented, rarely-changing interface into your code. Your private functions can be considered an implementation detail and may change over time, so are not safe to use directly.

    If a client programmer goes out of their way to circumvent these abstractions, they are in a way declaring that they know what they are doing. More importantly, they understand that it is unsupported and may stop working with future versions of your code.

提交回复
热议问题