interface Node<T extends Number> {
default public T getData() {
return (T) Integer.valueOf(1);
}
}
class MyNode<T extends Integer> implements Node<T> {
}
class FinalNode extends MyNode{
public static void main(String[] args) {
MyNode mn = new MyNode();
mn.getData();
}
}
This piece of code is just a simplification of the problem I faced in the huge code base. I extract it so it is easier to focus on the problem rather than the business logic :).
When I write the code, my IDE report me mn.getData()
return a Number
type. But mn
is an instance of class MyNode
, which is bounded by type "Integer
". So I expect mn.getData()
should return result with type "Integer
". It looks like the generic type bound is not working for inheritance. Could anyone give me some clue?
Modify the code. Is it also a raw type?
class MyNode implements Node<Integer> {
}
class FinalNode extends MyNode{
public static void main(String[] args) {
MyNode mn = new MyNode();
mn.getData();
}
}