1

Hi I want to implement a method on the lines of friend function in C++. How can I go about it?If it does not, why java doesn't need it?Please suggest how to implement it.. as in a sample:

public class A {//some variables and methods..private and protected to be used by methods in Class B}
public class B {}
garima
  • 5,154
  • 11
  • 46
  • 77
  • 1
    There ain't a friend function in Java. But if you give a use case, there must be a work-around. BTW, I do not like my `friends` to have access to my `private` members. (pun intended) – Nishant Jan 25 '11 at 06:25
  • Possible duplicate: http://stackoverflow.com/questions/4647599/why-friend-directive-is-missing-in-java/4647901 – Sergei Tachenov Jan 25 '11 at 06:35
  • no its not duplicate.I am asking a way to implement it in code – garima Jan 25 '11 at 06:42

2 Answers2

2

Yes, something does exist. Java classes declared in the same package can have access to their package-mates less-than-private members.

This is one of the places where Java and C++ really diverge and you need to get a specific understanding of the Java way to keep from shooting yourself in the foot.

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
  • Can't access private member. Can they? – Nishant Jan 25 '11 at 06:26
  • @Nishant, they can't, but there is another thing: nested and inner classes can access private members up to the topmost level class. – Sergei Tachenov Jan 25 '11 at 06:34
  • 2
    @Sergey Tachenov I agree. And, there is nothing that can be done in C++ using `friend` modifier, can't be done in Java using _something else_. What I am pointing here is, in Java, there isn't a single keyword or mechanism that gives you complete feature that `friend` of C++ gives. You need to choose tools (like one suggested in this answer or inner class or anything else) based on use-case that you're dealing with. – Nishant Jan 25 '11 at 06:43
  • I would just weigh in with my $0.02 and say that it is very easy to abuse they system (either Java or C++) by allowing your friends access to your "personal space" -- that doesn't make it a good idea. In practice, I have seen very few good reasons for friends, and very many horrid abuses. This sort of modifier should be culled from the lex pool. – Joe Zitzelberger Jan 25 '11 at 06:52
0

Having a friend in C++ actually violates OO Design principals. Its a convinience but then if you are pedantic its not the right way.

The better way in Java is as suggested in above posts i.e. have either inner classes or have them in same package.

Hope that helps.

Nilesh
  • 4,137
  • 6
  • 39
  • 53
  • 3
    I disagree, just in the same way that I disagree that the overzealous use of classes in Java makes it more object oriented than other languages. In particular, `java.utils.Collections` is just as object oriented as a C library --free standing functions. In the case of `friend`, they can help achieve all that OO preaches. The first example that comes to mind is a reference counted smart pointer and a weak pointer. They are related classes, and they need access to the same internal *reference count* type (and pointers during construction) – David Rodríguez - dribeas Jan 25 '11 at 08:53
  • 1
    Without the use of `friend` you would have to open access to the internal type and pointers to more code. If you are interested, we can discuss how `shared_ptr`s and `weak_ptr`s are implemented, why they do need access to each other's details and how not using `friend` would open access to a greater code base. – David Rodríguez - dribeas Jan 25 '11 at 08:55