Re: Why only public methods on interfaces?
ram@zedat.fu-berlin.de (Stefan Ram) writes:
ram@zedat.fu-berlin.de (Stefan Ram) writes:
A proxy could use several interfaces, one for public methods,
one for private methods. But ?private? to whom?
I want to give an example for what I was thinking about:
Two compilets (aka SSCCEs):
First: Make some methods of the interface callable only from
a certain scope in a way so that calls from other scopes
will be refuted already at compile time.
Second: Add undocumented behavior to a public class so that
the access can be obtained from a certain scope only by
passing a private key object at runtime.
First: Method ?example? can call ?method( Key )?, while
Method ?main? can't:
class Example
{
public static interface PublicInterface
{ public void method();
public /* private intended */ void method( Key parameter ); }
public static class Implementation implements PublicInterface
{
public void method()
{ java.lang.System.out.println( "public" ); }
public void method( final Key parameter )
{ java.lang.System.out.println( "private" ); }}
/* This is the key for access to the private Interface */
private static final class Key {}
public static void example()
{
/* Can get direct access to method( Key ) from here. */
Example.PublicInterface publicInterface = new Example.Implementation();
publicInterface.method( new Example.Key() ); }}
public class Main
{ public static void main( final java.lang.String[] args )
{
/* Can't get direct access to method( Key ) from here. */
Example.PublicInterface publicInterface = new Example.Implementation();
publicInterface.method();
Example.example(); }}
/* prints:
public
private
*/
Second: Method ?example? can call ?method( Key )?, while
Method ?main? can't:
interface PublicInterface
{ public void method( java.lang.Object parameter ); }
class Example implements PublicInterface
{
private interface PrivateInterface
{ public void privateMethod(); }
private static class PrivateImplementation implements PrivateInterface
{ public void privateMethod()
{ java.lang.System.out.println( "private" ); }}
final private PrivateImplementation privateImplementation =
new PrivateImplementation();
/* This is the key for access to the private Interface */
private static class Acceptor
{ private PrivateInterface privateInterface;
private void accept( final PrivateInterface privateInterface )
{ this.privateInterface = privateInterface; }
private PrivateInterface get()
{ return this.privateInterface; }}
@java.lang.Override
public void method( final java.lang.Object parameter )
{ if( parameter instanceof Acceptor )
{ final Acceptor acceptor =( Acceptor )parameter;
acceptor.accept( privateImplementation ); }
else
{ java.lang.System.out.println( "public" ); }}
public static void example()
{
/* Can get direct access to PrivateInterface from here. */
final PublicInterface publicInterface = new Example();
final Acceptor acceptor = new Acceptor();
publicInterface.method( acceptor );
PrivateInterface privateInterface = acceptor.get();
privateInterface.privateMethod(); }
}
public class Main
{
public static void main( final java.lang.String[] args )
{
/* Can't get direct access to PrivateInterface from here. */
PublicInterface publicInterface = new Example();
publicInterface.method( null );
( ( Example )publicInterface ).example( ); }}
/* prints:
public
private
*/