Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:05:24+00:00 2026-06-04T14:05:24+00:00

This covariance is possible in C#: IEnumerable<A> a = new List<A>(); IEnumerable<B> b =

  • 0

This covariance is possible in C#:

IEnumerable<A> a = new List<A>();
IEnumerable<B> b = new List<B>();

a = b;

...

class A {
}

class B : A {
}

This is not possible in Java: (Iterable: Seen in this question Java Arrays & Generics : Java Equivalent to C# IEnumerable<T>).

Iterable<A> a = new ArrayList<A>();
Iterable<B> b = new ArrayList<B>();

a = b;

...

class A {
}

class B extends A {
}

With Iterable, Java doesn’t see those two collection are covariance

Which iterable/enumerable interface in Java that can facilitate covariance?


Another good example of covariance, given the same A class and B class above, this is allowed on both Java and C# :

   A[] x;
   B[] y = new B[10];

   x = y;

That capability is on both languages from their version 1. It’s nice that they are making progress to make this a reality on generics. C# has lesser friction though in terms of syntax.

Covariance is a must on all OOP languages, otherwise OOP inheritance will be a useless exercise, e.g.

 A x;
 B y = new B();

 x = y;

And that power should extend to generics as well.



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(<? extends classHere>) certainly fits the bill:

import java.util.*;    
public class Covariance2 {  
  
        public static void testList(Iterable<? extends A> men) {                 
                for(A good : men) {
                        System.out.println("Good : " + good.name);
                }
        }    

        public static void main(String[] args) {    
                System.out.println("The A"); 
                {
                        List<A> team = new ArrayList<A>();
                        { A player = new A(); player.name = "John"; team.add(player); }
                        { A player = new A(); player.name = "Paul"; team.add(player); }
                        testList(team);
                }

                System.out.println("The B");
                {
                        List<B> bee = new ArrayList<B>();
                        { B good = new B(); good.name = "George"; bee.add(good); }
                        { B good = new B(); good.name = "Ringo"; bee.add(good); }
                        testList(bee);
                }    
        }
}

class A { String name; }    
class B extends A {}

Output:

The A
Good : John
Good : Paul
The B
Good : George
Good : Ringo

In case anyone are interested how it look like in C#

using System.Collections.Generic;
using System.Linq;

public class Covariance2 {  

        internal static void TestList(IEnumerable<A> men) {                 
                foreach(A good in men) {
                        System.Console.WriteLine("Good : " + good.name);
                }
        }    

        public static void Main(string[] args) {    
                System.Console.WriteLine("The A"); 
                {
                        IList<A> team = new List<A>();
                        { A player = new A(); player.name = "John"; team.Add(player); }
                        { A player = new A(); player.name = "Paul"; team.Add(player); }
                        TestList(team);
                }

                System.Console.WriteLine("The A"); 
                {
                        IList<B> bee = new List<B>();
                        { B good = new B(); good.name = "George"; bee.Add(good); }
                        { B good = new B(); good.name = "Ringo"; bee.Add(good); }
                        TestList(bee);
                }    
        }
}

class A { internal string name; }    
class B : A {}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:05:25+00:00Added an answer on June 4, 2026 at 2:05 pm

    Java generics allow covariance only if explicitly declared via wildcards in order to provide stricter type safety. This works:

        Iterable<? extends A> a = new ArrayList<A>();
        Iterable<B> b = new ArrayList<B>();
        a = b;
    

    However, note that you now cannot add anything via the reference a since it’s declared to contain instances of some specific but unknown class, which might be A or any subclass thereof. The behaviour of wildcards is often counter-intuitive and can get very complex, so they should be used in moderation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is the last line not allowed? IEnumerable<double> doubleenumerable = new List<double> { 1,
is this possible? if not, why isn't this possible in Java? interface B extends
Why does this not work? Do I not understand delegate covariance correctly? public delegate
Assuming class A { } class B : A { } covariance is not
Possible Duplicate: Question about C# covariance Why does the code compile in the first
Possible Duplicate: Difference between Covariance & Contra-variance I'm trying to understand what covariance and
Can you please explain why it is possible to do: import java.util.ArrayList; import java.util.List;
Im not sure how to state this question, because im not sure where the
I know that C# supports covariance in arrays like this : object[] array =
I was triggered by this SO question about (.NET 4.0) covariance and contravariance support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.