File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Nesting Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Nesting Interfaces" Watch "Nesting Interfaces" New topic
Author

Nesting Interfaces

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Could someone explaine to me what the following is ssaying?
"As a new twist, interfaces can also be private as seen in A.D (the same qualification syntax is used for nested interfaces as for nested classes). What good is a private nested interface? You might guess that it can only be implemented as a private nested class as in DImp, but A.DImp2 shows that it can also be implemented as a public class. However, A.DImp2 can only be used as itself. You are not allowed to mention the fact that it implements the private interface, so implementing a private interface is a way to force the definition of the methods in that interface without adding any type information (that is, without allowing any upcasting)." (-From Thinking in Java by Bruce Eckel)
In particular, I don't understand the bit "You are not allowed to mention the fact that it implements the private interface, so implementing a private interface is a way to force the definition of the methods in that interface without adding any type information (that is, without allowing any upcasting"
Thanks
FN
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
FN;
I don't have the book, but here's my take on the excerpt. Consider this code:
<pre><code>
class Outer {
public interface PublicInner {
void method1();
}
public class ImplPub implements PublicInner {
public void method1() {
System.out.println( "method1" );
}
}
private interface PrivateInner {
void method2();
}
public class ImplPriv implements PrivateInner {
public void method2() {
System.out.println( "method2" );
}
}
}
public class PrivateIntfTest {
public static void main( String[] args ) {
Outer.ImplPub implPub1 = new Outer().new ImplPub(); //ok
Outer.PublicInner implPub2 = new Outer().new ImplPub(); //assignment ok
Outer.PublicInner implPubA = (Outer.PublicInner)implPub1; //upcast ok
Outer.ImplPriv implPriv1 = new Outer().new ImplPriv(); //ok
Outer.PrivateInner implPriv2 = new Outer().new ImplPriv();//not allowed
Outer.PrivateInner implPrivA = (Outer.PrivateInner)implPriv1;//not allowed
}
}</code></pre>
You can refer to objects of type <code>ImplPub</code> through references of type <code>ImplPub</code> or <code>PublicInner</code>. You could also upcast a reference of type <code>ImplPub</code> to <code>PublicInner</code>. However, you can only refer to objects of type <code>ImplPriv</code> through references of that type, not of type <code>PrivateInner</code>. That type is not accessible,
and you're unable to upcast to it (even if you could create a reference of that type to assign to, you couldn't use the type in the cast).
IMO, this is pretty useless. Eckel knows his stuff, but he goes off on these pointless tangents involving extreme and torturous encapsulation. I think his point is that no user of your class need ever know about <code>PrivateInner</code> - maybe it's some precious intellectual property. Of course, since the interface is not accessible outside its enclosing class (which only the original programmer ever had access to), you could have just put the methods in <code>ImplPriv</code> and never defined the interface. The result would be the same. If you like this one, you should have seen some of the stuff in "Thinking in C++".
Now, maybe if you used <code>protected</code>...got to think about that one. Nope, that exposes the interface too much, and the point is to hide it.
It's a great brain exercise though, and I'm glad you posted it. This is the kind of question that keeps me hanging out on JR.
jply
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I was just curious! Thanks for the explanation. By the way, you can get a free online version of the book at:
http://www.mindview.net/TIJ2/index.html
FN
[This message has been edited by firstname lastname (edited September 15, 2000).]
 
wood burning stoves
 
subject: Nesting Interfaces
 
Similar Threads
Q from DAN's Nested class exam
what is the use of interface?
difference between abstract class and interface?
my notes on JLS for any1 who needs them !!
Can an Anonymous Inner Class implement more than one interface in Java 6