1

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

  • Does Java support an "internal" modifier like C# does? In C#, you can use internal to make things only visible within the same assembly. – OregonGhost Mar 05 '09 at 14:49
  • Really not enough information. Shouldn't serialisable methods be private anyway. – Tom Hawtin - tackline Mar 05 '09 at 14:49
  • Yeah you don't need access to the serialization methods to serialize objects. This seems argumentative and not a question either... – wds Mar 05 '09 at 15:01
  • @OregonGhost: No, Java doesn't support that. The nearest equivalent would be the default access modifier, which makes a class or member visible to the package in which it is defined. (A package in Java is most similar to a namespace in C#.) – Hosam Aly Mar 05 '09 at 15:05
  • JDK7 may (or may not) have a module access specifier. This would be different from internal in that it wont be dependent upon deployment unit. – Tom Hawtin - tackline Mar 05 '09 at 15:34

5 Answers5

6

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
  • 19,934
  • 4
  • 57
  • 70
2

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);
}
}
jhegedus
  • 20,244
  • 16
  • 99
  • 167
2

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
  • 145,806
  • 30
  • 211
  • 305
0

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
  • 60,850
  • 18
  • 118
  • 163
0

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.

Community
  • 1
  • 1
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246