tags:

views:

176

answers:

3

Ok, let's leave the debate of whether friendship breaks encapsulation, and actually try elegantly come up with a coherent design. It is a two fold function:

1) General question on how to implement:

   public class A 
   {
      friend class B;
   }

2) Why do I need this functionality? Some of my classes implement Serializable interface. However, I want to make Serializable methods protected in the Derived class so that I don't expose them to a client (as well as in the documentation -- javadoc). However, internal classes should be able to access them. What is the General way to solve this problem in java?

Note: I am using friendship as defined in the current C++ standard.

Thanks

+6  A: 

The general solution is to make the methods package-private (which is the default protection level in Java). That way any code in the same package can access them, but not external code.

Java does not allow arbitrary sharing of methods with specific external classes.

EDIT: Protected members are actually less private than package-private. If you have protected members, you can access them from derived classes outside your package, and from any class inside the package. So that may be a solution to your problem - derive the class in another class in the package you want to export to.

Generally, Java considers the package as the main module of encapsulation. The public/protected interface is for classes outside the package, and the default protection level allows access within the package.

Avi
well, I need to access them from other internal packages as well :(
Package-private (default access) should be quite rare for anything but top-level types.
Tom Hawtin - tackline
@Tom: I agree. I was just pointing out that it is the only officially endorsed way of sharing internals at all.
Avi
+2  A: 

It seems as if you want a façade.

You appear to have a class that needs to give public access to various other classes (even in different packages) involved in the implementation. But you don't want clients having access.

Therefore, make the implementation as complicated as you like. Have a façade class, with just the interface you want, delegate to the implementation.

Tom Hawtin - tackline
A: 

One thing I noticed... it sounds like you do not want the methods that you have to override because you are implementing Serializable to be made public. One thing though, Serializable is a marker interface (it provides no methods to be overridden) and the readObject and writeObject methods are supposed to be private.

Am I missing something?

TofuBeer