java 接口多重继承,Java接口上的多重继承

I thought multiple inheritance was always illegal in Java, but this code compiles:

public interface A {

void a();

}

public interface B {

void b();

}

public interface AB extends A, B {

}

Would having an empty interface such as AB be considered a bad practice? Is there a way to achieve something similar while avoiding the empty interface (using generics or otherwise)?

Note: I'm not asking how to simulate multiple inheritance via interfaces. I realize I could do the following:

public class AbImpl implements A, B {

public void a() {}

public void b() {}

}

For various reasons I need an interface that has both methods.

解决方案

Multiple inheritance of implementations is not allowed. Components can inherit multiple interfaces, though.

Inheriting multiple interfaces isn't problematic, since you're simply defining new method signatures to be implemented. It's the inheritance of multiple copies of functionality that is traditionally viewed as causing problems, or at the very least, confusion (e.g., the diamond of death).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值