0

I'm writing this simple java program with generic class. However, I have a problem while testing the program: Class "Student" extends class "Person". While creating ArrayBox (my generic class) I can't use Student class while creating the ArrayBox

I see error: "java: type argument com.company.Student is not within bounds of type-variable T"

the class Student extends class Person. I tried making it implement Comparable but then I have an error telling me that it can't inherit from different types (Extends person and implements C)

I've tried to make it extend Comparable but it doesn't seem to work either.

This is the part that shows me an error:

ArrayBox<Student> bst = new ArrayBox<>(1);

This is ArrayBox declaration:

public class ArrayBox<T extends Comparable<T>> {

Person class declaration:

public class Person implements Comparable<Person>{

Student class declaration:

public class Student extends Person{

I've searched the web but I still don't understand what can I do to make this work. This problem can be easily solved (or rather avoided) by using:

ArrayBox<Person> bst = new ArrayBox<>(1);

instead of:

ArrayBox<Student> bst = new ArrayBox<>(1);

But I really need this work with the second form. How should class "Student" be declared in order to make this code work?

Sorry if my question seems chaotic as it is my first time asking here.

2
  • what objects are you trying to add in the ArrayBox<Student> bst = new ArrayBox<>(1);?
    – user10386912
    Mar 29, 2019 at 9:50
  • @ValentinCarnu I'm trying to add Student class objects. Inside ArrayBox class there's a method to add them to an array. It all works well as long as there's ArrayBox<Person> ... Mar 29, 2019 at 9:52

2 Answers 2

1

A possible way is to change a bit the ArrayBox signature to something like:

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

The above will make ArrayBox open to extensions of T.

4
  • Oh wow, that worked! Thank you very much! However, is there any way to do it without changing ArrayBox signature? Mar 29, 2019 at 10:15
  • One option, is to use the implements Comparable<Student> as daniu mentioned in another answer, but then you get the problem you mentioned. To avoid that problem you would have to remove the implements Comparable from Person, but then all the extensions of Person would have to repeat the comparison logic. An alternative is to use a Comparator instead of Comparable. But that would require changes to ArrayBox anyway.
    – rph
    Mar 29, 2019 at 10:30
  • Alrigh, I will try removing Comparable implementation from Person class. Thank you for your answer! Mar 29, 2019 at 10:47
  • @someUser1605: This answer is the correct way to do it. Because Comparable is a consumer (it only has a method which takes T as a parameter), it should always be used with ? super bounds due to the PECS (producer extends, consumer super). Bounds should be specified in the least restrictive way that guarantees the functionality you need, and you just need T to be comparable to T -- specifically, that T's compareTo method takes a T -- and you don't need the compareTo's parameter to be exactly T for that; any supertype of T would also be fine.
    – newacct
    Apr 7, 2019 at 5:05
0

The reason is that Student extending Person makes it a Comparable<Person>, but not a Comparable<Student> which is what your ArrayBox expects.

It should be enough to let it implement Comparable<Student>.

1
  • It won't work unfortunately. Then I have an error: java comparable cannot be inherited with different arguments Mar 29, 2019 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.