I want to implements a generic interface in a class. Consider implementing this generic interface:
public interface Lookup<T>{
public T find(String name);
}
and this is the not generic class that implement Lookup:
public class IntegerLookup implements Lookup<Integer>{
private Integer[] values;
private String[] names;
public IntegerLookup(String[] names, Integer[] values) {......}
//now I want to write the implemented method - find
and my question is: how do I need to write this implemented method? I need to override it? yes?
public Object find(String name){...}
will be good? or:
public Integer find(String name){...}
The syntax here
public class IntegerLookup implements Lookup<Integer>{
// ^
binds the type argument provided, ie. Integer
, to the type variable declared by Lookup
, ie. T
.
So,
public Integer find(String name){...}
This is the same as doing
Lookup<Integer> lookup = ...;
lookup.find("something"); // has a return type of Integer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With