Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class<? super T> in getSuperclass() Does it make sense?

Class<? super T>  getSuperclass()

The getSuperclass() in Class class return a Class whose type is<? super T>, which mean that the type parameter of Class of the Super could be T or any of its Superclasses, now how come a SuperClass's Class type parameter be the same type as the subclass?

for example

Class<Manage>.class.getSuperclass() 

doesn't return

Class<Manager> 

at all ..like never

Does this make sense? or i am missing something here?

like image 534
skystar7 Avatar asked Dec 15 '22 16:12

skystar7


2 Answers

The bound is just a overly-broad because it's as close as you can express using Java's generics.

What you really want is <the immediate superclass of T>; but there's no way to write that in Java's generics. There's also no way to write <? super T excluding T>. <? super T> is pretty much as specific as you can get given the way Java's generics work.

For what it's worth you can guarantee that the class you get back will be the superclass in question -- no matter what its type bound is.

like image 70
Calum Avatar answered Jan 06 '23 06:01

Calum


There's no way with Java generics to specify "some superclass but not the class itself." So the provided bound is the best one you can possibly specify.

like image 27
Louis Wasserman Avatar answered Jan 06 '23 06:01

Louis Wasserman