I was wondering why Java has been designed without the friend
directive that is available in C++ to allow finer control over which methods and instance variables are available from outside the package in which a class has been defined.
I don't see any practical reason nor any specific drawback, it seems just a design issue but something that wouldn't create any problem if added to the language.
friend
can be used to break encapsulation, so can other features be misused. Used correctly,friend
enhances encapsulation because it enables a more fine-grained access control:friend
replaces use ofpublic
, not use ofprivate
. Technical explanation at the C++ FAQ (That said, I am wholly satisfied with package visibility, but claiming thatfriend
breaks encapsulation is still wrong.) – Deyo