Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 27
    friends -> "The less you know about it the better" ---> It gives selective visibility, which is still superior to package privacy. In C++, it has its uses, because not all functions can be member functions, and friends is better than public'ing. Of course there is a danger of misuse by evil minds. Commented Jun 7, 2011 at 10:13
  • 34
    It should also be noted that "protected" in C++ has a different meaning - a protected method is effectively private, but can still be called from an inheriting class. (As opposed to Java where it can be called by any class within the same package.) Commented Oct 2, 2011 at 12:34
  • 9
    @RhysvanderWaerden C# is the same as C++ in this aspect. I find it pretty odd that Java doesn't allow to declare a member that's accessible to the subclass but not the entire package. It's sort of upside down to me - a package is broader scope than a child class! Commented Oct 15, 2013 at 17:36
  • 16
    @KonradMorawski IMHO package is smaller scope than subclass. If you haven't declared your class final, users should be able to subclass it - so java protected is part of your published interface. OTOH, packages are implicitly developed by a single organization: e.g. com.mycompany.mypackage. If your code declares itself in my package, you implicitly declare yourself part of my organization, so we should be communicating. Thus, package publishes to a smaller/easier to reach audience (people in my company) than subclass (people who extend my object) and so counts as lower visibility. Commented May 22, 2014 at 20:37
  • 3
    friend is good for defining special relationships between classes. It allows superior encapsulation in many cases when used correctly. For example it can be used by a privileged factory class to inject internal dependencies into a constructed type. It has a bad name because people who don't care about correctly maintaining a well designed object model can abuse it to ease their workload. Commented Dec 8, 2014 at 10:05