Reputation: 5285
How can I declare a method signature that takes Long
, Integer
as input?
I tried following but it gives a compilation error:
List<Myobject> fun ( ? extends Number) value){
//impl
}
Upvotes: 1
Views: 137
Reputation: 8261
Simply
private <T extends Number> List<Myobject> fun(T value) {
// do anything with the value
return aMyobjectList;
}
Upvotes: 0
Reputation: 32831
Why not simply declare your argument as Number?
List<Myobject> fun (Number value){
//impl
}
Upvotes: 1
Reputation: 38225
<N extends Number> List<MyObject> fun(N value) { ... }
But in this use-case, I don't really see the benefit of using generics. I mean, why doesn't your method simply take a Number
as argument ?
Upvotes: 0
Reputation: 115378
This method takes any Number as input:
private <N extends Number> void foo(N param) {}
This one also returns List of the same type
private <N extends Number> List<N> foo(N param) {}
Upvotes: 1