does anything on the lines of friend function exist in java?

526 Views Asked by At

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 {}
2

There are 2 best solutions below

4
On BEST ANSWER

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.

2
On

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.