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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:44:48+00:00 2026-05-14T18:44:48+00:00

Why can I use super only with wildcards and not with type parameters? For

  • 0

Why can I use super only with wildcards and not with type parameters?

For example, in the Collection interface, why is the toArray method not written like this

interface Collection<T>{
    <S super T> S[] toArray(S[] 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-05-14T18:44:49+00:00Added an answer on May 14, 2026 at 6:44 pm

    super to bound a named type parameter (e.g. <S super T>) as opposed to a wildcard (e.g. <? super T>) is ILLEGAL simply because even if it’s allowed, it wouldn’t do what you’d hoped it would do, because since Object is the ultimate super of all reference types, and everything is an Object, in effect there is no bound.

    In your specific example, since any array of reference type is an Object[] (by Java array covariance), it can therefore be used as an argument to <S super T> S[] toArray(S[] a) (if such bound is legal) at compile-time, and it wouldn’t prevent ArrayStoreException at run-time.

    What you’re trying to propose is that given:

    List<Integer> integerList;
    

    and given this hypothetical super bound on toArray:

    <S super T> S[] toArray(S[] a) // hypothetical! currently illegal in Java
    

    the compiler should only allow the following to compile:

    integerList.toArray(new Integer[0]) // works fine!
    integerList.toArray(new Number[0])  // works fine!
    integerList.toArray(new Object[0])  // works fine!
    

    and no other array type arguments (since Integer only has those 3 types as super). That is, you’re trying to prevent this from compiling:

    integerList.toArray(new String[0])  // trying to prevent this from compiling
    

    because, by your argument, String is not a super of Integer. However, Object is a super of Integer, and a String[] is an Object[], so the compiler still would let the above compile, even if hypothetically you can do <S super T>!

    So the following would still compile (just as the way they are right now), and ArrayStoreException at run-time could not be prevented by any compile-time checking using generic type bounds:

    integerList.toArray(new String[0])  // compiles fine!
    // throws ArrayStoreException at run-time
    

    Generics and arrays don’t mix, and this is one of the many places where it shows.


    A non-array example

    Again, let’s say that you have this generic method declaration:

    <T super Integer> void add(T number) // hypothetical! currently illegal in Java
    

    And you have these variable declarations:

    Integer anInteger
    Number aNumber
    Object anObject
    String aString
    

    Your intention with <T super Integer> (if it’s legal) is that it should allow add(anInteger), and add(aNumber), and of course add(anObject), but NOT add(aString). Well, String is an Object, so add(aString) would still compile anyway.


    See also

    • Java Tutorials/Generics
      • Subtyping
      • More fun with wildcards

    Related questions

    On generics typing rules:

    • Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()?
    • java generics (not) covariance
    • What is a raw type and why shouldn’t we use it?
      • Explains how raw type List is different from List<Object> which is different from a List<?>

    On using super and extends:

    • Java Generics: What is PECS?
      • From Effective Java 2nd Edition: “producer extends consumer super“
    • What is the difference between super and extends in Java Generics
    • What is the difference between <E extends Number> and <Number>?
    • How can I add to List<? extends Number> data structures? (YOU CAN’T!)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can use a standard dot notation or a method call in Objective-C to
You can use App.config; but it only supports key/value pairs. You can use .Net
You can use more than one css class in an HTML tag in current
I can use properties of an Excel Worksheet to tell if the worksheet is
You can use ftplib for full FTP support in Python. However the preferred way
You can use SelectFolder() to get a folder or GetOpenFolderitem(filter as string) to get
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
I can use FlashWindowEx to make a window flash in the taskbar, but what
You can use XPath if you're binding the XML document in the XAML, but
You can use command lsof to get file descriptors for all running processes, but

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.