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

  • Home
  • SEARCH
  • 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 6880761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:56+00:00 2026-05-27T05:00:56+00:00

I have a generic java class that stores comparables: public class MyGenericStorage<T extends Comparable<T>>

  • 0

I have a generic java class that stores comparables:

public class MyGenericStorage<T extends Comparable<T>> {
    private T value;

    public MyGenericStorage(T value) {
        this.value = value;
    }

    //... methods that use T.compareTo()
}

I also have an abstract class called Person:

public abstract class Person implements Comparable<Person>

and two concrete subclasses, Professor and Student:

public class Professor extends Person
public class Student extends Person

now when I want to create a MyGenericStorage like so, I get an error:

//error: type argument Student is not within bounds of type-variable T
MyGenericStorage<Student> studStore = new MyGenericStorage<Student>(new Student());

//this works: 
MyGenericStorage<Person> persStore = new MyGenericStorage<Person>(new Student());

I think this is because I have a fundamental problem with understanding generics. Can someone explain this to me, and also, how to fix it?

EDIT:

I have changed MyGenericStorage to the following:

public class MyGenericStorage<T extends Comparable<? super T>> 

and now it seems to work. Can someone explain why?

  • 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-27T05:00:56+00:00Added an answer on May 27, 2026 at 5:00 am

    You can fix this with the following declaration for MyGenericStorage:

    class MyGenericStorage<T extends Comparable<? super T>> { …
    

    This means that T must have a Comparable implementation that accepts some supertype of T. In the case of Student and Professor, the supertype represented by the bound (?) is Person.


    Update: “now it seems to work. Can someone explain why?”

    Well, I tried in my original answer, but let me give it another shot.

    ? super T means “some supertype of T”. Suppose T in this case is Student. So, Student must implement Comparable “for some supertype of Student”

    Student extends Person, which implements Comparable<Person>. So, Student does indeed implement Comparable “for some supertype of Student”.

    If you have questions about Java Generics, the best place to start is Angelika Langer’s FAQ. In this case, the entry about bounded wild-cards may be helpful.

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

Sidebar

Related Questions

I have generic type that looks like: public class GenericClass<T, U> where T :
I've got a problem with generic classes in java. I've got this class: public
In this simplified example I have a generic class, and a method that returns
I know Java's generics are somewhat inferior to .Net's. I have a generic class
What methods do I need to add to a custom Java class so that
I have jar files which I constructed using command jar -cf Generic.jar /java/mypackage/*.class There
I have a (Java) class with operations like this: abstract class Holder { def
I have a generic class which represents a fragment of text. That fragment of
In Java, it's convenient for Class to have the generic parameter, X. But C#'s
I have a legacy class that the class itself is not a generic 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.