St.Antario
St.Antario

Reputation: 27385

Generic type subclass

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?

Upvotes: 2

Views: 69

Answers (2)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

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.

Upvotes: 10

NiziL
NiziL

Reputation: 5140

This one should work :)

public class NumberEvaluator<T extends Number> extends Evaluator<T> {
}

Upvotes: 4

Related Questions