File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does Sun exposes private methods through reflection

 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using reflection, we can invoke the private method of another class.

I am bit curious to know why Sun had this feature implemented. Doesn't this defeat encapsulation?
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was discussed some time back here
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sunil Vasudevan:
Doesn't this defeat encapsulation?


A class that would be 100% encapsulated would be worthless, because you couldn't communicate with it. With other words, there always is a balance between encapsulation and other forces. Sometimes that means that violating encapsulation to the point of accessing a private field is *the right to do*!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic