Alex Rivera | Logout

Java generic type

Asked 2010-01-29T22:24:43.937
12

When I have an interface

public interface Foo<T> {
    T someMethod();
}

is there any way to assure that when some class implements this interface then generic type is the same implementig class.

For example:

public class Bar implements Foo<Bar>  {
    Bar someMethod() {
        return new Bar();
    }
}
Edit
Report

1 Answer

0

This cannot be done because of Type Erasure.

EDIT :

I got down voted, here is more explanation :

public interface Foo<T>;

T from above is available in run time, so it cannot be checked. However, one could custom annotation that could enforce this.

answered 2010-01-29T22:31:11.997

Your Answer