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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:22:26+00:00 2026-06-03T18:22:26+00:00

I am confused by the generic subtyping. In Java, if type A is a

  • 0

I am confused by the generic subtyping.

In Java, if type A is a subtype of B, generic type C<A> and C<B> are invariant. For instance, ArrayList<Base> is not a subtype of ArrayList<Derived>.

However, in Scala, generic type C<A> and C<B> are covariant if type A is a subtype of B. So what’s the property of generic class in Scala has but not in Java?

  • 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-03T18:22:28+00:00Added an answer on June 3, 2026 at 6:22 pm

    Firstly note that variance is a property of generic type parameters, not of the parameterized types themselves.

    Secondly: you are wrong about scala – type parameters are invariant by default. Let us investigate!

    Java

    Java has use-site variance annotations. That is, you can declare methods like this:

    boolean addAll(Collection<? extends T> c);
    

    There is, however, one form of “parameterized type” (I use the term loosely) in which the type parameters are covariant: Java Arrays! (This is actually insane because Arrays are mutable and hence it is easy to circumvent the type system). Consider the following:

    public static void subvert(Object[] arr) { arr[0] = "Oh Noes!"; }
    

    And then:

    Integer[] arr = new Integer[1];
    subvert(arr); //this call is allowed as arrays are covariant
    Integer i = arr[0];
    

    A good interview question this one: what happens?

    Scala

    In Scala, you have declaration-site variance. That is, the variance of a type parameter is declared alongside the parameter (using the annotations + and -):

    trait Function1[-I, +O]
    

    This says that the trait Function1 has two type parameters, I and O which are contra- and co-variant respectively. If no +/- annotation is declared, then the type parameter is invariant. For example, Set is invariant in its type parameter:

    scala> def foo(set: Set[Any]) = ()
    foo: (set: Set[Any])Unit
    
    scala> Set(1)
    res4: scala.collection.immutable.Set[Int] = Set(1)
    
    scala> foo(res4)
    <console>:10: error: type mismatch;
     found   : scala.collection.immutable.Set[Int]
     required: Set[Any]
    Note: Int <: Any, but trait Set is invariant in type A.
    You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
                  foo(res4)
                      ^
    

    List is however, declared as being covariant:

    scala> def bar(list: List[Any]) = ()
    bar: (list: List[Any])Unit
    
    scala> List(1)
    res6: List[Int] = List(1)
    
    scala> bar(res6)
    

    Why not ask the compiler?

    Another way of demonstrating this is to ask the compiler directly for subtype-evidence:

    scala> class Cov[+A]
    defined class Cov
    
    scala> implicitly[Cov[Int] <:< Cov[Any]]
    res8: <:<[Cov[Int],Cov[Any]] = <function1>
    

    But with an invariant type parameter

    scala> class Inv[A]
    defined class Inv
    
    scala> implicitly[Inv[Int] <:< Inv[Any]]
    <console>:9: error: Cannot prove that Inv[Int] <:< Inv[Any].
                  implicitly[Inv[Int] <:< Inv[Any]]
                            ^
    

    Lastly, contravariance:

    scala> class Con[-A]
    defined class Con
    
    scala> implicitly[Con[Any] <:< Con[Int]]
    res10: <:<[Con[Any],Con[Int]] = <function1>
    

    See also identifier <:<

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

Sidebar

Related Questions

I'm a bit confused by the difference in type-checking between curried and uncurried generic
This is not generic question. I have asked this question because I'm confused with
I'm learning generics in Java from C++ and am confused how to do math
Bit confused here, I have an on-demand instance but do I get charged even
I am writing a generic repository for entity framework and am confused as to
I'm really confused as to why my URL dispatcher is not matching this url
I've just got confused how to implement something in a generic way in C++.
In relation to Casting generic type "as T" whilst enforcing the type of T
I am a new java learner. Recently I was reading Generic programming and got
I got a bit confused, because when I use the generic ListView class, Django

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.