为什么在这种情况下使用Interface over Abstract类?

Cod*_*mer 3 java inheritance interface

我在这里阅读博客文章:http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/

public interface Actor{
   Performance say(Line l);
}
public interface Director{
   Movie direct(boolean goodmovie);
}
public interface ActorDirector extends Actor, Director{
...
}
Run Code Online (Sandbox Code Playgroud)

它说:实际上,有演员也是导演.如果我们使用接口而不是抽象类.我们可以使用抽象类来实现相同的功能.不幸的是,替代方案需要最多2 ^ n(其中n是属性的数量)可能的组合以便支持所有可能性.

问题:为什么抽象类在这里更好?为什么2 ^ n?

public abstract class ActorDirector implements Actor,Director{
}
Run Code Online (Sandbox Code Playgroud)

Att*_*ila 6

您不能从多个类(抽象或不抽象)继承,但可以实现多个接口

如果要创建抽象类,则每个组合都需要一个(n个属性:2 ^ n可能的组合) - 以确保每个组合都可以继承,如果需要的话.

如果使用接口,则只需定义n接口(每个属性一个)并根据需要实现它们.