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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:08:11+00:00 2026-06-13T17:08:11+00:00

Consider this small test class: import java.util.List; public abstract class Test { // CAN

  • 0

Consider this small test class:

import java.util.List;

public abstract class Test {

    // CAN modify this constructor interface
    public <T extends Runnable & Comparable<T>> Test(List<T> l) {
        setList((List<Runnable>)l);   // <-- (a) warning
        setList(l);                   // <-- (b) error
    }

    // CANNOT modify this interface
    public abstract void setList(List<Runnable> l);
}

This succinctly represents my problem, in that I want to use generic methods which take objects of type T (which are both Runnable and Comparable<T>) like Test‘s constructor, but I am constrained to using other methods with interfaces like that of setList to take collections of T.

(a) Why does the compiler warn about an unchecked conversion to List<Runnable> when l is an instance of List<T> where T extends, and has type erasure, of Runnable (as per JLS SE7 §4.6)?

(b) The compiler raises the following error:

error: method setList in class Test cannot be applied to given types;
    setList(l);   // <-- compiler error
    ^   
required: List<Runnable>   
found: List<T>   reason: actual argument List<T> cannot be converted to 
                         List<Runnable> by method invocation conversion   
where T is a type-variable:
    T extends Runnable,Comparable<T> declared in constructor <T>Test(List<T>)

My understanding here is that method invocation conversion couldn’t convert T to Runnable because perhaps this is considered a narrowing operation, but this is counter-intuitive, as I expected it could as T intersects both Runnable and Comparable<T>.

Must I resort to unchecked conversions (a) of instances of T to List<Runnable> in this case?

EDIT

The answer turns out to be yes, as Bhesh has pointed out below, generic types are invariant in Java. If I could use the following, error (b) would not occur:

public abstract void setList(List<? extends Runnable> l);

For more information, see this excellent tutorial and this section on capture conversion in the JLS SE7 §5.1.10.

  • 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-13T17:08:12+00:00Added an answer on June 13, 2026 at 5:08 pm

    a) T is-a Runnable but List<T> is-not-a List<Runnable>. Generic types are invariant in Java.

    b) Reason is the same as the above. Method parameters are also invariant in Java. (Note: The method arguments are covariant.)


    The cast in setList((List<Runnable>)l); should be safe, because we know that T is always going to be something that implements Runnable. This is the type of situation for which the annotation @SuppressWarnings("unchecked") exists.


    Still a problem could be (depending upon your situation) that you are invoking an overridable method from a constructor.

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

Sidebar

Related Questions

Consider this small piece of code from apscheduler.scheduler import Scheduler import time class First():
Consider this small program: #include <stdio.h> #include <stdlib.h> // Change 60000 to 70000 and
I'm having difficulty understanding variable shadowing in JavaScript based on scopes. Consider this small
I've posted some questions on this before, but it's different. So consider a small
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Consider this scenario. I have a small enterprise project dev environment. We use archiva
Consider this small snippet of JavaScript: for(var i in map.maps) { buttons.push($(<button>).html(i).click(function() { alert(i);
Consider this (snipped) example <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html xmlns=http://www.w3.org/1999/xhtml>>
Hy everyone, pls consider this small code, and help me to figure out, why

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.