轉自鳥窩 博主寫的挺詳細,不瞭解的看一看啊 html
之前常常談論的Java對比c++的一個優點是Java中沒有多繼承的問題。 由於Java中子類只能繼承(extends)單個父類, 儘管能夠實現(implements)多個接口,可是接口中只有抽象方法,方法體是空的,沒有具體的方法實現,不會有方法衝突的問題。java
這些都是久遠的說法了,自從今年Java 8發佈後, 接口中也能夠定義方法了(default method)。 之因此打破之前的設計在接口中
增長具體的方法, 是爲了既有的成千上萬的Java類庫的類增長新的功能, 且沒必要對這些類從新進行設計。 好比, 只需在Collection接口中
增長default Stream<E> stream()
, 相應的Set
和List
接口以及它們的子類都包含此的方法, 沒必要爲每一個子類都從新copy這個方法。c++
這是一個折衷的設計,帶來的問題就是爲Java引入了多繼承的問題。 咱們知道, 接口能夠繼承接口, 類能夠繼承類和實現接口。 一旦繼承的類和實現的接口中有
相同簽名的方法, 會出現什麼樣的情況呢? 本文將探討各類狀況的多繼承, 以便能清楚的理解Java多繼承的規則。
oracle
假定有三個接口Interface A, Interface B, Interface C, 繼承關係以下:ide
1
2
3
4
5
6
7
8
9
|
+---------------+ +------------+
| Interface A | |Interface B |
+-----------^---+ +---^--------+
| |
| |
| |
+-+------------+--+
| Interface C|
+------------+
|
A,B擁有相同簽名的默認方法default String say(String name)
, 若是接口C沒有override這個方法, 則編譯出錯。spa
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這個方法, 這樣編譯就不會出錯了:code
1
2
3
4
5
|
interface C extends A,B{
default String say(String name) {
return "greet " + name;
}
}
|
注意方法簽名不包括方法的返回值, 也就是僅僅返回值不一樣的兩個方法的簽名也是相同的。
下面的代碼編譯不會出錯,由於A
和B
的默認方法不一樣, C隱式繼承了兩個默認方法。htm
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
。
能夠看出, 子類優先繼承父類的方法, 若是父類沒有相同簽名的方法,才繼承接口的默認方法。