Re: Why only public methods on interfaces?
ram@zedat.fu-berlin.de (Stefan Ram) writes:
public InterfaceB getPrivateInterface( privateClassA token );
[amendment: ?privateClassA? should have been written
with an uppercase ?P?.]
Or, a hack as a last ressort:
If one has a public method in InterfaceA such as
public ... methodA( ..., SomeClassA object, ... );
in the interface, and SomeClassA is not final, we can
extend ?SomeClassB? by a private class ?PrivateClassA?.
The method ?methodA? will check at run time, whether ?object?
is of this private class ?PrivateClassA?. If so, it will call
a method from ?PrivateClassA?:
object.acceptPrivateInterface( myPrivateInterfaceObject )
so that now the caller has a reference to the private
interface object.
The hack is not visible in the public interface InterfaceA
and it is impossible for a client out of the scope of
?PrivateClassA? to observe this behavior as long as the
implementation does not release such a token object to the
outside. While this is not documented in ?InterfaceA? nor
?methodA?, it could be documented in ?PrivateClassA? and
of course in the source code of ?methodA? via Java comments.