Assign a subclass of a Generic class to a super class of this class

后端 未结 5 2108
心在旅途
心在旅途 2020-12-11 19:58

I have couple of supplied interfaces

public interface Finder,T extends Item> {
    public S find(S s, int a);
}

public interf         


        
5条回答
  •  既然无缘
    2020-12-11 20:15

    public class SimpleFinder,T extends Item> implements Finder{
        public S find(S s, int a){
            Stack stack = ....;
            ...
            stack = s.getCopy();
            .....
            return (S) stack;
        }
    }
    

    should work. Keep in mind that stack must be a Stack and not S to match getCopy() return type. I would expect S type to be Ok, since it extends Stack, but implementing it this is the behavior that I'm observing.

提交回复
热议问题