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 6931855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:54+00:00 2026-05-27T11:37:54+00:00

Today, I read some articles about Covariance, Contravariance (and Invariance) in Java. I read

  • 0

Today, I read some articles about Covariance, Contravariance (and Invariance) in Java. I read the English and German Wikipedia article, and some other blog posts and articles from IBM.

But I’m still a little bit confused on what these exactly are about? Some say it’s about relationship between types and subtypes, some say it’s about type conversion and some say it’s used to decide whether a method is overridden or overloaded.

So I’m looking for an easy explanation in plain English, that shows a beginner what Covariance and Contravariance (and Invariance) is. Plus point for an easy example.

  • 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-05-27T11:37:55+00:00Added an answer on May 27, 2026 at 11:37 am

    Some say it is about relationship between types and subtypes, other say it is about type conversion and others say it is used to decide whether a method is overwritten or overloaded.

    All of the above.

    At heart, these terms describe how the subtype relation is affected by type transformations. That is, if A and B are types, f is a type transformation, and ≤ the subtype relation (i.e. A ≤ B means that A is a subtype of B), we have

    • f is covariant if A ≤ B implies that f(A) ≤ f(B)
    • f is contravariant if A ≤ B implies that f(B) ≤ f(A)
    • f is invariant if neither of the above holds

    Let’s consider an example. Let f(A) = List<A> where List is declared by

    class List<T> { ... } 
    

    Is f covariant, contravariant, or invariant? Covariant would mean that a List<String> is a subtype of List<Object>, contravariant that a List<Object> is a subtype of List<String> and invariant that neither is a subtype of the other, i.e. List<String> and List<Object> are inconvertible types. In Java, the latter is true, we say (somewhat informally) that generics are invariant.

    Another example. Let f(A) = A[]. Is f covariant, contravariant, or invariant? That is, is String[] a subtype of Object[], Object[] a subtype of String[], or is neither a subtype of the other? (Answer: In Java, arrays are covariant)

    This was still rather abstract. To make it more concrete, let’s look at which operations in Java are defined in terms of the subtype relation. The simplest example is assignment. The statement

    x = y;
    

    will compile only if typeof(y) ≤ typeof(x). That is, we have just learned that the statements

    ArrayList<String> strings = new ArrayList<Object>();
    ArrayList<Object> objects = new ArrayList<String>();
    

    will not compile in Java, but

    Object[] objects = new String[1];
    

    will.

    Another example where the subtype relation matters is a method invocation expression:

    result = method(a);
    

    Informally speaking, this statement is evaluated by assigning the value of a to the method’s first parameter, then executing the body of the method, and then assigning the methods return value to result. Like the plain assignment in the last example, the “right hand side” must be a subtype of the “left hand side”, i.e. this statement can only be valid if typeof(a) ≤ typeof(parameter(method)) and returntype(method) ≤ typeof(result). That is, if method is declared by:

    Number[] method(ArrayList<Number> list) { ... }
    

    none of the following expressions will compile:

    Integer[] result = method(new ArrayList<Integer>());
    Number[] result = method(new ArrayList<Integer>());
    Object[] result = method(new ArrayList<Object>());
    

    but

    Number[] result = method(new ArrayList<Number>());
    Object[] result = method(new ArrayList<Number>());
    

    will.

    Another example where subtyping matters is overriding. Consider:

    Super sup = new Sub();
    Number n = sup.method(1);
    

    where

    class Super {
        Number method(Number n) { ... }
    }
    
    class Sub extends Super {
        @Override 
        Number method(Number n);
    }
    

    Informally, the runtime will rewrite this to:

    class Super {
        Number method(Number n) {
            if (this instanceof Sub) {
                return ((Sub) this).method(n);  // *
            } else {
                ... 
            }
        }
    }
    

    For the marked line to compile, the method parameter of the overriding method must be a supertype of the method parameter of the overridden method, and the return type a subtype of the overridden method’s one. Formally speaking, f(A) = parametertype(method asdeclaredin(A)) must at least be contravariant, and if f(A) = returntype(method asdeclaredin(A)) must at least be covariant.

    Note the “at least” above. Those are minimum requirements any reasonable statically type safe object oriented programming language will enforce, but a programming language may elect to be more strict. In the case of Java 1.4, parameter types and method return types must be identical (except for type erasure) when overriding methods, i.e. parametertype(method asdeclaredin(A)) = parametertype(method asdeclaredin(B)) when overriding. Since Java 1.5, covariant return types are permitted when overriding, i.e. the following will compile in Java 1.5, but not in Java 1.4:

    class Collection {
        Iterator iterator() { ... }
    }
    
    class List extends Collection {
        @Override 
        ListIterator iterator() { ... }
    }
    

    I hope I covered everything – or rather, scratched the surface. Still I hope it will help to understand the abstract, but important concept of type variance.

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

Sidebar

Related Questions

I read some articles about HTTP headers compression. Today I installed YSlow and it
Today I read this blog entry by Roger Alsing about how to paint a
I read today about sharded counters in Google App Engine . The article says
I'm thinking about security problems, made some questions, read some articles..... it's too difficult
I read about the Conditional attribute today. According to MSDN: Applying ConditionalAttribute to a
I've read some texts about declarative/functional programming (languages), tried out Haskell as well as
Today, I've had some debate with my colleague about choosing data types in our
I read plenty of articles about Azure and .NET Service Bus. I think I
Today my question is about date formats and strings. My application downloads some strings
I have some questions about the usage of the close() method when using Java

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.