interface A {
default void say(int name) {
}
}
interface B {
default void say(String name) {
}
}
interface C extends A,B{
}
但是有的情况下即使是不同签名的方法也是很难分辨的:
interface A {
default void say(int a) {
System.out.println("A");
}
}
interface B {
default void say(short a) {
System.out.println("B");
}
}
interface C extends A,B{
}
static class D implements C {
}
public static void main(String[] args) {
D d = new D();
byte a = 1;
d.say(a); //B
}
Java会选择最适合的方法, 请参看Java规范 15.12.2.5
接口多层继承
下面看一下多层继承的问题。 继承关系如下图, A2继承A1, C继承A2。
+---------------+
| Interface A1 |
+--------+------+
|
|
|
+--------+------+
| Interface A2 |
+-------+-------+
|
|
|
+-------+--------+
| Interface C |
+----------------+
基于我们以前对类继承的认识, 很容易知道C会继承A2的默认方法,包括直接定义的默认方法, 覆盖的默认方法,以及隐式继承于A1接口的默认方法。
interface A {
default void say(int a) {
System.out.println("A");
}
default void run() {
System.out.println("A.run");
}
}
interface B extends A{
default void say(int a) {
System.out.println("B");
}
default void play() {
System.out.println("B.play");
}
}
interface C extends A,B{
}
多层多继承
上面一个例子还是单继承的例子, 如果如下图的多继承呢?
+---------------+
| Interface A1 |
+--------+------+
|
|
|
+--------+------+ +---------------+
| Interface A2 | | Interface B |
+-------+-------+ +---------+-----+
| +---------+---------^
| |
| |
+-------+-------++
| Interface C |
+----------------+
如果A2和B拥有相同签名的方法,这和第一个例子一样。 如果不想编译出错,可以覆盖父接口的默认方法,还可以调用指定父接口的默认方法:
interface A1 {
default void say(int a) {
System.out.println("A1");
}
}
interface A2 extends A1 {
}
interface B {
default void say(int a) {
System.out.println("B");
}
}
interface C extends A2,B{
default void say(int a) {
B.super.say(a);
}
}
更复杂的多层多继承
+--------------+
| Interface A1 |
+------+------++
| ^+-------+
| |
+-------+-------+ |
| Interface A2 | |
+------------+--+ |
^--++ |
| |
+--+------+-----+
| Interface C |
+---------------+
接口A2继承A1, 接口C继承A2和A1。 代码如下,
interface A1 {
default void say() {
System.out.println("A1");
}
}
interface A2 extends A1 {
default void say() {
System.out.println("A2");
}
}
interface C extends A2,A1{
}
static class D implements C {
}
public static void main(String[] args) {
D d = new D();
d.say();
}
以上代码不会编译出错,运行输出A2。
可以看到接口C会隐式继承子接口的方法, 也就是子接口A2的默认方法。
类继承
如果继承关系类型全部是类, 那么由于类依然是单继承的, 不会有多继承的问题。
类和接口混杂
我们把第一个例子中的其中一个接口换成类,会出现什么现象呢。
+-------------+ +-----------+
| Interface A | | Class B |
+-----------+-+ +-----+-----+
^-+ +--+-----^
| |
+---+----+-+
| Class C |
+----------+
以下代码不会编译出错:
interface A {
default void say() {
System.out.println("A");
}
}
static class B {
public void say() {
System.out.println("B");
}
}
static class C extends B implements A{
}
public static void main(String[] args) {
C c = new C();
c.say(); //B
}
结果输出B。
可以看出, 子类优先继承父类的方法, 如果父类没有相同签名的方法,才继承接口的默认方法。
结论
更复杂的继承关系可以简化成以上的继承关系。
根据以上的例子, 可以得出以下的结论:
类优先于接口。 如果一个子类继承的父类和接口有相同的方法实现。 那么子类继承父类的方法
子类型中的方法优先于父类型中的方法。
如果以上条件都不满足, 则必须显示覆盖/实现其方法,或者声明成abstract。
22/2<12