does anything on the lines of friend function exist in java?
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 {}
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.
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.
精彩评论