akshay
akshay

Reputation: 5285

Method signature that takes any parameter as input

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

Answers (5)

Kowser
Kowser

Reputation: 8261

Simply

private <T extends Number> List<Myobject> fun(T value) {
    // do anything with the value
    return aMyobjectList;
}

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

Why not simply declare your argument as Number?

List<Myobject> fun (Number value){
 //impl       

}

Upvotes: 1

Costi Ciudatu
Costi Ciudatu

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

AlexR
AlexR

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

Alpedar
Alpedar

Reputation: 1344

public <N extends Number>List<T>  xx(N a) {...}

Upvotes: 3

Related Questions