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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:03:44+00:00 2026-05-26T20:03:44+00:00

When compiling this import java.util.List; public class Test { public static class Subject {

  • 0

When compiling this

import java.util.List;

public class Test {
    public static class Subject {
    }

    public static class MySubject extends Subject {
    }

    public static class Step<TSubjectA extends Subject> {
        public void doWork(TSubjectA subject) {
        }
    }

    public static class Worker {
        public static <TSubjectB extends Subject> void doWork(TSubjectB subject, List<Step<? extends TSubjectB>> steps) {
            for (Step<? extends TSubjectB> step : steps) {
                step.doWork(subject);
            }
        }
    }

    public static void main(String[] args) {
        MySubject subject;
        List<Step<? extends Subject>> steps;

        Worker.doWork(subject, steps);
    }
}

I get the error

Test.java:18: doWork(capture#95 of ? extends TSubjectB) in Test.Step cannot be applied to (TSubjectB)

From what I can see, subject is of type TSubjectB, which extends Subject. The type TSubjectA is a type which extends TSubjectB which extends Subject. So I should be able to pass subject to doWork and it should all be typesafe.

Am I missing something and it is actually not typesafe, or is this just a limitation of Java’s generics?

  • 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-26T20:03:44+00:00Added an answer on May 26, 2026 at 8:03 pm

    While the type parameters TSubjectA and TSubjectB are each guaranteed to be some type extending Subject, they aren’t necessarily the same type – one could be MySubject and the other could be MyOtherSubject. This is the reason the current code isn’t considered typesafe by the compiler.

    If the functionality you require is strictly dependent on the Subject base implementation, just remove the type parameters and rely on polymorphism:

    public static class Step {
        public void doWork(Subject subject) {
           //do Subject stuff
        }
    }
    
    public static class Worker {
        public static void doWork(Subject subject, List<Step> steps) {
            for (Step step : steps) {
                step.doWork(subject);
            }
        }
    }
    
    public static void main(String[] args) {
    
        List<Step> steps = ...;
    
        //all valid
        Worker.doWork(new Subject(), steps);
        Worker.doWork(new MySubject(), steps);
        Worker.doWork(new MyOtherSubject(), steps);
    }
    

    If it isn’t, that is if the doWork implementation is different depending on the subtype of Subject, then generics won’t help you and you’ll need to make some design changes. Keep in mind that generics in Java aren’t dynamic magic, but rather syntactic sugar to aid compile-time type safety. Read up on type erasure if you haven’t already to understand this limitation.

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

Sidebar

Related Questions

Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
import java.io.*; public class ArrayApp{ public static void main(String[] args){ System.out.println(lllll); } // end
java Source code not compiling with jdk 6. import org.w3c.dom.Node; Node node = list.item(0);
Having problems compiling this class. I'm working through a book on java and this
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
While compiling a program in Java I got this big WARNING warning: [unchecked] unchecked
I am trying to get a test class compiling with JUnit, and I can't
I need to import a Java class in my project. The class I need
Compiling this lines long int sz; char tmpret[128]; //take substring of c, translate in

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.