Why can't I implement multiple interfaces?

MWB*_*MWB 6 java interface implements

I am creating a game where objects implement interfaces for animations. I have a parent interface for the animations. Here is a shortened version:

public interface Animates<S extends Animator> {
    S createAnimator(long animationTime);
}
Run Code Online (Sandbox Code Playgroud)

In addition, I have multiple interfaces that extend this interface. Two examples:

public interface AnimatesPaint extends Animates<PaintAnimator> {
    PaintAnimator createPaintAnimator(long animationTime);

    default PaintAnimator createAnimator(long animationTime) {
        return createPaintAnimator(animationTime);
    }

}
Run Code Online (Sandbox Code Playgroud)

and

public interface AnimatesPosition extends Animates<PositionAnimator> {
    PositionAnimator createPositionAnimator(long animationTime);

    @Override
    default PositionAnimator createAnimator(long animationTime) {
        return createPositionAnimator(animationTime);
    }

}
Run Code Online (Sandbox Code Playgroud)

As you can see, the interfaces that extend Animates override the createAnimator method in order to delegate the logic of createAnimator to the class that implements the interface.

The reason I am doing this is that I want to be able to have a screen element (that can animate) that implements multiple animation interfaces (for example both AnimatesPosition to move the element around and AnimatesPaint to change its color).

However, that apparently does not work. When I implement both in one class (illustrated below), I get the compilation error:

createAnimator(long) in AnimatesPaint clashes with createAnimator(long) in AnimatesPosition; attempting to use incompatible return types

Here is an example of a class that implements both Animates interfaces:

public class ScreenElement implements AnimatesPaint, AnimatesPosition {
    @Override
    PositionAnimator createPositionAnimator(long animationTime) {
        return new PositionAnimator(animationTime);
    }
    @Override
    PaintAnimator createPaintAnimator(long animationTime) {
        return new PaintAnimator(animationTime);
    }
}
Run Code Online (Sandbox Code Playgroud)

So what I don't understand is that both AnimatesPaint and AnimatesPosition already implement createAnimator. Yet, the error message seems to suggest that createAnimator also needs to be implemented by ScreenElement! If createAnimator would not have been implemented yet, I would get it, why there is a clash.

Where does my logic go wrong?

In the end, what I want to achieve, is to have a generic method that can initiate any type of animation. For example:

public class AnimationStarter<S extends Animates, T> {

    public void startAnimation(S animates, T start, T target, long animationTime) {
        Animator animator = animates.createAnimator(animationTime);
        animator.init(start, target);
        animates.setAnimator(animator);
        animator.startAnimation();
    }
}
Run Code Online (Sandbox Code Playgroud)

--Edit--

Upon request, here is the declaration of Animator

public abstract class Animator<T> {}
Run Code Online (Sandbox Code Playgroud)

及其扩展类之一

public class PositionAnimator extends Animator<Point>{}
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 4

所以我不明白的是两者AnimatesPaintAnimatesPosition已经实现了createAnimator

是的,这些实现相互冲突。如果您可以这样做,则生成的类的类型将需要公开两个createAnimator仅通过返回类型区分的方法。Java 不允许您拥有仅通过返回类型区分的重载,因此您不能这样做。出于重载目的,方法签名不包括返回类型。

即使它们具有相同的返回类型 ( Animator),您也会有两个具有完全相同签名的重载,但您不能这样做。

如果它们要在同一个类中实现,则它们需要是单独的方法(例如,具有可以区分的单独签名)。


在您提出的评论中:

但冲突不是因为该方法已经被AnimatesPaintand重写了AnimatesPosition吗?这样实现类ScreenElement不需要实现createAnimator方法,所以不会发生冲突。

不,因为类本身将这些方法公开(或者更确切地说,需要)作为其签名的一部分。基本上,假设您可以创建该类并且您有它的一个实例,s. 会做什么呢s.createAnimator(300L)?编译器应该选择哪一个?

类的公共类型由其所有公共成员组成,包括它实现的所有接口的所有公共成员。因此,在类型级别,两个接口不可能实现具有相同签名的方法。