Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

limits to the invariance principle?

843793Jan 11 2007 — edited Jan 15 2007
let's have this
MyClass implements MyInterface
now I cannot write:
Iterator<MyInterface> iter;
Iterator<MyClass> iter2 = ..... ;
iter = iter2 ;// DOES NOT COMPILE : INVARIANCE PRINCIPLE
hey... wait a minute are we really breaking type safety by doing that?
I suspect not ...
I even suspect that rules such as : "if a parameterized interface uses its parameters only in the return type of methods then the invariance principle does not apply" could be true (in principle though not in implementation)...
I may be wrong .... your advice? any counter-example to satisfy me?
going further I think it should be legal to write this:
Iterable<MyInterface> iter = iterableMyClass ;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 12 2007
Added on Jan 11 2007
26 comments
144 views