简体   繁体   中英

Java bounded type parameters in interface implementation

I want to combine the use of bounded type parameters with DI spring. That is, I would like to declare an interface:

public abstract class BaseClass {
}

public class ChildClass extends BaseClass {
}

public interface SomeInterface {
    <T extends BaseClass> void update(T impl);
}

and make its implementation:

@Service
public class Impl1 extends SomeInterface {
    @Override
    void update(ChildClass impl) {
    }
}

That is, I want to declare in the interface a general type condition for implementations and use a specific type of successor in the implementation.

Is it possible?

You can:

public interface SomeInterface<T extends BaseClass> {

    void update(T impl);

}

and then:

@Service
public class Impl1 extends SomeInterface<ChildClass> {
    @Override
    void update(ChildClass impl) {

    }
}

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