Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java generic template error

Tags:

java

generics

Why do i get a compilation error on eclipse with the following definition in an interface:
Area is an interface.

public interface Shape {
     ...  
     public Comparator<T extends Area> getComparator();
}

and not if I use instead:

public interface Shape {
     ... 
     public Comparator<? extends Area> getComparator();
}
like image 700
Captain Jack sparrow Avatar asked Jan 27 '26 01:01

Captain Jack sparrow


2 Answers

Because the compiler has no idea what T is supposed to be or represent. Now, if you had something like public interface Shape<T> as the interface declaration, we could probably get something to work with that.

like image 112
ziesemer Avatar answered Jan 28 '26 20:01

ziesemer


T isn't defined in the code sample you've shown.

The following should be legal:

public interface Shape {
     ...  
     public <T extends Area> Comparator<T> getComparator();
}

or:

public interface Shape<T extends Area> {
     ...  
     public Comparator<T> getComparator();
}
like image 37
RMorrisey Avatar answered Jan 28 '26 22:01

RMorrisey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!