Question

This question already has an answer here:

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

This link gives a way to emulate friend access in Java: http://macchiato.com/columns/Durable7.html

The code copied from the link (in case the site is not accessible):

public class A {
private int privateInt = 31415;

public class SomePrivateMethods {
    public int getSomethingPrivate() { return privateInt; }
    private SomePrivateMethods() {} // no public constructor
}
public void giveKeyTo(B other) {
    other.receiveKey(new SomePrivateMethods());
}
}

public class B {
private A.SomePrivateMethods key;

public void receiveKey(A.SomePrivateMethods key) {
    this.key = key;
}

public void usageExample() {
    A anA = new A();

    //int foo = anA.privateInt; // doesn't work, not accessible

    anA.giveKeyTo(this);
    int fii = key.getSomethingPrivate();
    System.out.println(fii);
}
}

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?

There are two solutions to your question that don't involve keeping all classes in the same package.

The first is to use the Friend Accessor/Friend Package pattern described in (Practical API Design, Tulach 2008).

The second is to use OSGi. There is an article here explaining how OSGi accomplishes this.

Related Questions: 1, 2, 3, and 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top