title: casting Collection<SomeClass> to Collection<SomeSuperClass>
body: <p>I'm sure this has been answered before, but I really cannot find it.</p>
  
  <p>I have a java class <code>SomeClass</code> and an abstract class <code>SomeSuperClass</code>. <code>SomeClass</code> extends <code>SomeSuperClass</code>.</p>
  
  <p>Another abstract method has a method that returns a <code>Collection&lt;SomeSuperClass&gt;</code>. In an implementation class, I have a <code>Collection&lt;SomeClass&gt; myCollection</code></p>
  
  <p>I understand that I cannot just return <code>myCollection</code>, because <code>Collection&lt;SomeClass&gt;</code> does not inherit from <code>Collection&lt;SomeSuperClass&gt;</code>. Nevertheless, I know that everything in <code>myCollection</code> is a <code>SomeSuperClass</code> because after all, they're <code>SomeClass</code> objects which extend <code>SomeSuperClass</code>.</p>
  
  <p>How can I make this work?</p>
  
  <p>I.e. I want</p>
  
  <pre><code>public class A
  {
    private Collection&lt;SomeClass&gt; myCollection;
  
    public Collection&lt;SomeSuperClass&gt; getCollection()
    {
      return myCollection; //compile error!
    }
  }
  </code></pre>
  
  <p>The only way I've found is casting via a non-generic type and getting unchecked warnings and whatnot. There must be a more elegant way, though? I feel that also using <code>Collections.checkedSet()</code> and friends are not needed, since it is statically certain that the returned collection only contains <code>SomeClass</code> objects (this would not be the case when downcasting instead of upcasting, but that's not what I'm doing). What am I missing?</p>
  
  <p>Thanks!</p>