简体   繁体   中英

Java override method with nested classes generic parameters

I'm trying to override the following methods but I can't figure out the generics mechanism:

public interface MyInterface<T extends MyGenericObject> {
    public void read(long id, Callback<T> callback);
    public void readAll(Callback<Collection<T>> callback);
}

public class SomeObject extends MyGenericObject {
    ...
}

public class MyClass implements MyInterface<SomeObject> {
    @Override
    public void read(long id, **????** callback) {
        ...
    }

    @Override
    public void readAll(**????** callback) {
        ...
    }
}

Could you please help me?

EDIT: I had an intermediate Interface between MyClass and MyInterface and the generic SomeObject wasnt passed all the way, that was my problem...

Just replace each T by SomeObject which is passed to MyInterface<T> .

// T has become SomeObject = put everywhere SomeObject instead of T
public class MyClass implements MyInterface<SomeObject> { 
    @Override
    public void read(long id, Callback<SomeObject> callback) {
        ...
    }

    @Override
    public void readAll(Callback<Collection<SomeObject>> callback) {
        ...
    }
}

EDIT

You have mentioned in comment that it does not compile due of erasure problem (why you getting this error is explained in comment section), however this is not possible with code you have posted:

public interface MyInterface<T extends MyGenericObject> {
    void read(long id, Callback<T> callback);
    void readAll(Callback<Collection<T>> callback);
}

public interface Callback<T> {}
public class MyGenericObject {}
public class SomeObject extends MyGenericObject {}

public class MyClass implements MyInterface<SomeObject> {
    @Override public void read(long id, Callback<SomeObject> callback) {}
    @Override public void readAll(Callback<Collection<SomeObject>> callback) {}
}

This code compiles without any error:

import java.util.Collection;

interface Callback<T> {}

class MyGenericObject {}

interface MyInterface<T extends MyGenericObject> {
    public void read(long id, Callback<T> callback);
    public void readAll(Callback<Collection<T>> callback);
}

class SomeObject extends MyGenericObject { }

class MyClass implements MyInterface<SomeObject> {
    @Override
    public void read(long id, Callback<SomeObject> callback) {
    }

    @Override
    public void readAll(Callback<Collection<SomeObject>> callback) {
    }
}

If you are getting "read/readAll in MyClass clashes with read/readAll in MyInterface; both methods have same erasure, yet neither overrides the other", then your code is different to what you have indicated.

可以肯定的是,您只能分别使用Callback<SomeObject> callbackCallback<Collection<SomeObject>> callback

Try

public class MyClass<T extends  SomeObject> implements MyInterface<T> {

Now you can use T in your method implementations. You will specify the specific class when you instantiate MyClass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM