Is this a good/right way to use generic interface?
I have a generic interface like:
I can implement it like:
1.
2.
3.
4.
IMO, all implementations above are valid, but:
* implementation 1 treat generic as old code before generic imported into Java, compiler will give a warning on it, but at least it's clear to most of all
* implementation 2 is weird in fact, it's same with implementation 1, but it will give a shock to user
* implementation 3 use generic interface in a not bad way, but it use Object on one of methods prototype, and I don't think it's better for user either
* implementation 4 use generic interface in a right way
What's your insight?
public interface Ia<E> {
public void add(E element);
public E get();
}
I can implement it like:
1.
public class A implements Ia {
public void add(Object element) {
}
public Object get() {
return null;
}
}
2.
public class A implements Ia<Object> {
public void add(Object element) {
}
public Object get() {
return null;
}
}
3.
public class A<F> implements Ia<F> {
public void add(Object element) {
}
public F get() {
return null;
}
}
4.
public class A<F> implements Ia<F> {
public void add(F element) {
}
public F get() {
return null;
}
}
IMO, all implementations above are valid, but:
* implementation 1 treat generic as old code before generic imported into Java, compiler will give a warning on it, but at least it's clear to most of all
* implementation 2 is weird in fact, it's same with implementation 1, but it will give a shock to user
* implementation 3 use generic interface in a not bad way, but it use Object on one of methods prototype, and I don't think it's better for user either
* implementation 4 use generic interface in a right way
What's your insight?