Re: Why only public methods on interfaces?
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
--232016332-1378577239-1302454285=:14871
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; FORMAT=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <alpine.DEB.2.00.1104101753221.14871@urchin.earth.li>
On Sun, 10 Apr 2011, Stefan Ram wrote:
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.
[snip]
public class Main
{ public static void main( final java.lang.String[] args )
{
/* Can't get direct access to method( Key ) from here. */
Yes you can:
publicInterface.method(null);
For this technique to be effective, there has to be a null check in the
method. Even then, it will be a runtime error, not a compile-time error,
when it is called from outside the intended scope.
Your second technique does work, though, because it requires the caller to
be able to provide an instance of the hidden Acceptor type. Another way of
using that, which i think gives you static safety, is:
public class PublicFace {
public class PrivateFace { // intended private
public void secret() {}
}
private PrivateFace hyde = new PrivateFace();
private static class Extractor {
public static final Extractor THIS = new Extractor();
public PrivateFace extract(PublicFace jekyll) {
return jekyll.hyde;
}
}
public static void insider(PublicFace jekyll) {
Extractor.THIS.extract(jekyll).secret();
}
}
public class Main {
public static void outsider(PublicFace jekyll) {
// i claim that there is no way to write an expression of type Extractor here
// without that, you cannot possibly extract hyde from jekyll
}
}
tom
--
I am the best at what i do.
--232016332-1378577239-1302454285=:14871--