Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic type subclass

Tags:

java

generics

I have the following generic class:

public class Evalutor<T>{

}

I would like to create the type called NumberEvalutor as follows:

public class NumberEvalutor<T> extends Evalutor<T extends Number>{ //Syntax error on token "extends", , expected

}

But I couldn't do it that way. Maybe you can advice another type-safe way?

like image 388
St.Antario Avatar asked Aug 12 '26 04:08

St.Antario


1 Answers

Try with:

public class NumberEvalutor<T extends Number> extends Evalutor<T> { 

}

Type parameters on class-level (like <T extends Number>) must be introduced after the class name and can be referred in the super-class/super-interface list. Otherwise, there won't be a way to (explicitly) specify their runtime value when creating class instances.

like image 73
Konstantin Yovkov Avatar answered Aug 14 '26 09:08

Konstantin Yovkov



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!