Visibility of interface
If class implements private interface that extends public interface, it is possibe to cast such a class to a private (sub)interface. Why?
Example,
publicclass C{
publicinterface A{
publicvoid foo();
}
}
class CC{
interface Bextends C.A{
publicvoid goo();
}
}
publicclass Class1implements CC.B{
publicvoid foo(){}
publicvoid goo(){}
}
//antother package
publicclass Main{
publicstaticvoid main(String[] args){
C.A obj=(C.A)new Class1();
}
}
Class1 implements interface CC.B that is not visible to the Main. CC.B extends C.A that is visible to Main. Why it is possible to cast Class1 to the C.A? I guess "it is not a bug, it is feature", but why?
[2101 byte] By [
alexsmaila] at [2007-10-2 5:53:00]

> If class implements private interface that extends
> public interface, it is possibe to cast such a class
> to a private (sub)interface. Why?
No it's not.
> Example,
>
> >
> public class C {
>public interface A{
>public void foo();
>}
> }
>
> class CC {
>interface B extends C.A {
>public void goo();
>}
> }
>
> public class Class1 implements CC.B {
>public void foo(){}
>public void goo(){}
> }
>
> //antother package
> public class Main {
>
>public static void main(String[] args) {
>C.A obj=(C.A)new Class1();
>}
> }
>
>
> Class1 implements interface CC.B that is not visible
> to the Main. CC.B extends C.A that is visible to
> Main. Why it is possible to cast Class1 to the C.A?
> I guess "it is not a bug, it is feature", but why?
This isn't doing what you mentioned in your first sentence before the example, it's casting to a public interface not a private interface. You can cast a class to any of it's supertypes. Whether or not the implementation itself is visible is irrelevent.
> > If class implements private interface that extends
> > public interface, it is possibe to cast such a
> class
> > to a private (sub)interface. Why?
>
> No it's not.
I had a mistake. I meant public (sub)interface.
>
> > Example,
> >
> > > >
> > public class C {
> >public interface A{
> >public void foo();
> >}
> > }
> >
> > class CC {
> >interface B extends C.A {
> >public void goo();
> >}
> > }
> >
> > public class Class1 implements CC.B {
> >public void foo(){}
> >public void goo(){}
> > }
> >
> > //antother package
> > public class Main {
> >
> >public static void main(String[] args) {
> >C.A obj=(C.A)new Class1();
> >}
> > }
> >
> >
> > Class1 implements interface CC.B that is not
> visible
> > to the Main. CC.B extends C.A that is visible to
> > Main. Why it is possible to cast Class1 to the
> C.A?
> > I guess "it is not a bug, it is feature", but
> why?
>
> This isn't doing what you mentioned in your first
> sentence before the example, it's casting to a public
> interface not a private interface. You can cast a
> class to any of it's supertypes. Whether or not the
> implementation itself is visible is irrelevent.
My question is why it is possible to cast to ""hidden" type. That is type that is accessible from a function, but implementation class doesn't implement this interface directly, but inderectly throw the private type. I would expect, that such casting willn't succeed. What was a reason to allow such casting?
> My question is why it is possible to cast to
> o ""hidden" type. That is type that is accessible
> from a function, but implementation class doesn't
> implement this interface directly, but inderectly
> throw the private type. I would expect, that such
> casting willn't succeed. What was a reason to allow
> such casting?
You can attempt to cast any Object reference to any Object type. Whether it succeeds depends only on whether the instance at runtime is actually an instance of the type you are casting to. In this case, the returned Object obvious is an instance of C.A.
The real question here is: why would you expect this to fail? Are you surprised when you get a String out of a List and the cast to String doesn't fail?
> I had a mistake. I meant public (sub)interface.
You're missing the point.
> My question is why it is possible to cast to
> o ""hidden" type. That is type that is accessible
> from a function, but implementation class doesn't
> implement this interface directly, but inderectly
> throw the private type. I would expect, that such
> casting willn't succeed. What was a reason to allow
> such casting?
Let's try this again. You are NOT casting to a hidden type. The visibility of the implementation is irrelevent because you are not casting to it. You are saying one thing and then doing another. You say "Why does it let me cast to a type not visible?" when you are not doing that, you are casting to a type that is visible. This would be a proper example of what you're saying:
public interface A {}
interface B extends A {}
public class X implements B {}
// from another package
public class Y {
public static void main(String[] args) {
X aClass = new X();
// doesn't fail, this is what you were doing
A someA = (A)aClass;
// Fails
B someB = (B)aClass;
}
}
Let me ask you this, for any class is this valid:
Object o = (Object)aClass;
Yes it is. Does it matter whether or not aClass is an instance of a type not visible? No. Why would it?
kablair, dubwai, thanks, I got it.> You can attempt to cast any Object reference to any> Object type. Whether it succeeds depends only on> whether the instance at runtime is actually an> instance of the type you are casting to.