轉自鳥窩 博主寫的挺詳細,不了解的看一看啊
以前經常談論的Java對比c++的一個優勢是Java中沒有多繼承的問題。 因為Java中子類只能繼承(extends)單個父類, 盡管可以實現(implements)多個接口,但是接口中只有抽象方法,方法體是空的,沒有具體的方法實現,不會有方法沖突的問題。
這些都是久遠的說法了,自從今年Java 8發布后, 接口中也可以定義方法了(default method)。 之所以打破以前的設計在接口中
增加具體的方法, 是為了既有的成千上萬的Java類庫的類增加新的功能, 且不必對這些類重新進行設計。 比如, 只需在Collection接口中
增加default Stream<E> stream()
, 相應的Set
和List
接口以及它們的子類都包含此的方法, 不必為每個子類都重新copy這個方法。
這是一個折衷的設計,帶來的問題就是為Java引入了多繼承的問題。 我們知道, 接口可以繼承接口, 類可以繼承類和實現接口。 一旦繼承的類和實現的接口中有
相同簽名的方法, 會出現什么樣的狀況呢? 本文將探討各種情況的多繼承, 以便能清楚的理解Java多繼承的規則。
接口繼承多個父接口
假定有三個接口Interface A, Interface B, Interface C, 繼承關系如下:
1
2
3
4
5
6
7
8
9
|
+---------------+ +------------+
| Interface A | |Interface B |
+-----------^---+ +---^--------+
| |
| |
| |
+-+------------+--+
| Interface C|
+------------+
|
A,B擁有相同簽名的默認方法default String say(String name)
, 如果接口C沒有override這個方法, 則編譯出錯。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
interface A {
default String say(String name) {
return "hello " + name;
}
}
interface B {
default String say(String name) {
return "hi " + name;
}
}
interface C extends A,B{
}
|
錯誤信息:
1
2
3
4
5
6
|
C:\Lambda\src>javac -J-Duser.country=US com\colobu\lambda\chap
ter3\MultipleInheritance1.java
com\colobu\lambda\chapter3\MultipleInheritance1.java:
17: error: interface C inherits unrelated defaults for say(String) from types A and B
static interface C extends A,B{
^
1 error
|
我們可以在子接口C
中覆蓋override這個方法, 這樣編譯就不會出錯了:
1
2
3
4
5
|
interface C extends A,B{
default String say(String name) {
return "greet " + name;
}
}
|
注意方法簽名不包括方法的返回值, 也就是僅僅返回值不同的兩個方法的簽名也是相同的。
下面的代碼編譯不會出錯,因為A
和B
的默認方法不同, C隱式繼承了兩個默認方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
interface A {
default void say(int name) {
}
}
interface B {
default void say(String name) {
}
}
interface C extends A,B{
}
|
但是有的情況下即使是不同簽名的方法也是很難分辨的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
+---------------+
| Interface A1 |
+--------+------+
|
|
|
+--------+------+
| Interface A2 |
+-------+-------+
|
|
|
+-------+--------+
| Interface C |
+----------------+
|
基於我們以前對類繼承的認識, 很容易知道C會繼承A2的默認方法,包括直接定義的默認方法, 覆蓋的默認方法,以及隱式繼承於A1接口的默認方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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{
}
|
多層多繼承
上面一個例子還是單繼承的例子, 如果如下圖的多繼承呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
+---------------+
| Interface A1 |
+--------+------+
|
|
|
+--------+------+ +---------------+
| Interface A2 | | Interface B |
+-------+-------+ +---------+-----+
| +---------+---------^
| |
| |
+-------+-------++
| Interface C |
+----------------+
|
如果A2和B擁有相同簽名的方法,這和第一個例子一樣。 如果不想編譯出錯,可以覆蓋父接口的默認方法,還可以調用指定父接口的默認方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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);
}
}
|
更復雜的多層多繼承
1
2
3
4
5
6
7
8
9
10
11
12
13
|
+--------------+
| Interface A1 |
+------+------++
| ^+-------+
| |
+-------+-------+
|
| Interface A2 | |
+------------+--+
|
^--++
|
| |
+--+------+-----+
| Interface C |
+---------------+
|
接口A2繼承A1, 接口C繼承A2和A1。 代碼如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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的默認方法。
類繼承
如果繼承關系類型全部是類, 那么由於類依然是單繼承的, 不會有多繼承的問題。
類和接口混雜
我們把第一個例子中的其中一個接口換成類,會出現什么現象呢。
1
2
3
4
5
6
7
8
|
+-------------+ +-----------+
| Interface A | | Class B |
+-----------+-+ +-----+-----+
^-+ +--+-----^
| |
+---+----+-+
| Class C |
+----------+
|
以下代碼不會編譯出錯:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
。
可以看出, 子類優先繼承父類的方法, 如果父類沒有相同簽名的方法,才繼承接口的默認方法。