title: What's the equivalent of C# IEnumerable in Java? The covariant-capable one, not the Iterable
body: <p>This covariance is possible in C#:</p>
  <pre><code>IEnumerable&lt;A&gt; a = new List&lt;A&gt;();
  IEnumerable&lt;B&gt; b = new List&lt;B&gt;();
  
  a = b;
  
  ...
  
  class A {
  }
  
  class B : A {
  }
  </code></pre>
  <p>This is not possible in Java: (Iterable: Seen in this question <a href="https://stackoverflow.com/questions/362367/java-arrays-generics-java-equivalent-to-c-sharp-ienumerablet">Java Arrays &amp; Generics : Java Equivalent to C# IEnumerable&lt;T&gt;</a>).</p>
  <pre><code>Iterable&lt;A&gt; a = new ArrayList&lt;A&gt;();
  Iterable&lt;B&gt; b = new ArrayList&lt;B&gt;();
  
  a = b;
  
  ...
  
  class A {
  }
  
  class B extends A {
  }
  </code></pre>
  <p>With Iterable, Java doesn't see those two collection are covariance</p>
  <p>Which iterable/enumerable interface in Java that can facilitate covariance?</p>
  <hr/>
  <p>Another good example of covariance, given the same A class and B class above, this is allowed on both Java and C# :</p>
  <pre><code>   A[] x;
     B[] y = new B[10];
  
     x = y;
  </code></pre>
  <p>That capability is on both languages from their version 1. It's nice that they are making progress to make this a reality on <a href="https://stackoverflow.com/a/10664932">generics</a>. C# has lesser friction though in terms of syntax.</p>
  <p>Covariance is a must on all OOP languages, otherwise OOP inheritance will be a useless exercise, e.g.</p>
  <pre><code> A x;
   B y = new B();
  
   x = y;
  </code></pre>
  <p>And that power should extend to generics as well.</p>
  <hr/>
  <hr/>
  <p>Thanks everyone for the answer and insights. Got now a reusable method with covariant-capable Java generics now. It's not the syntax that some of us wants, but it(<code>&lt;? extends classHere&gt;</code>) certainly fits the bill:</p>
  <pre><code>import java.util.*;    
  public class Covariance2 {  
    
          public static void testList(Iterable&lt;? extends A&gt; men) {                 
                  for(A good : men) {
                          System.out.println(&quot;Good : &quot; + good.name);
                  }
          }    
  
          public static void main(String[] args) {    
                  System.out.println(&quot;The A&quot;); 
                  {
                          List&lt;A&gt; team = new ArrayList&lt;A&gt;();
                          { A player = new A(); player.name = &quot;John&quot;; team.add(player); }
                          { A player = new A(); player.name = &quot;Paul&quot;; team.add(player); }
                          testList(team);
                  }
  
                  System.out.println(&quot;The B&quot;);
                  {
                          List&lt;B&gt; bee = new ArrayList&lt;B&gt;();
                          { B good = new B(); good.name = &quot;George&quot;; bee.add(good); }
                          { B good = new B(); good.name = &quot;Ringo&quot;; bee.add(good); }
                          testList(bee);
                  }    
          }
  }
  
  class A { String name; }    
  class B extends A {}
  </code></pre>
  <p>Output:</p>
  <pre><code>The A
  Good : John
  Good : Paul
  The B
  Good : George
  Good : Ringo
  </code></pre>
  <hr>
  <p>In case anyone are interested how it look like in C#</p>
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  
  public class Covariance2 {  
  
          internal static void TestList(IEnumerable&lt;A&gt; men) {                 
                  foreach(A good in men) {
                          System.Console.WriteLine(&quot;Good : &quot; + good.name);
                  }
          }    
  
          public static void Main(string[] args) {    
                  System.Console.WriteLine(&quot;The A&quot;); 
                  {
                          IList&lt;A&gt; team = new List&lt;A&gt;();
                          { A player = new A(); player.name = &quot;John&quot;; team.Add(player); }
                          { A player = new A(); player.name = &quot;Paul&quot;; team.Add(player); }
                          TestList(team);
                  }
  
                  System.Console.WriteLine(&quot;The A&quot;); 
                  {
                          IList&lt;B&gt; bee = new List&lt;B&gt;();
                          { B good = new B(); good.name = &quot;George&quot;; bee.Add(good); }
                          { B good = new B(); good.name = &quot;Ringo&quot;; bee.Add(good); }
                          TestList(bee);
                  }    
          }
  }
  
  class A { internal string name; }    
  class B : A {}
  </code></pre>