java中的接口是類的藍(lán)圖。 它有靜態(tài)常量和抽象方法。java中的接口是一種實(shí)現(xiàn)抽象的機(jī)制。 接口中只有抽象方法而不是方法體。接口用于在Java中實(shí)現(xiàn)抽象和多重繼承。
Java接口也表示IS-A關(guān)系。它不能像抽象類一樣被實(shí)例化。
需要使用接口主要有三個(gè)原因。如下面給出。
由于Java 8,接口可以有默認(rèn)和靜態(tài)方法,稍后討論。
由編譯器內(nèi)部添加
java編譯器在接口方法之前添加public和abstract關(guān)鍵字。還有,它在數(shù)據(jù)成員之前添加public,static和final關(guān)鍵字。
換句話說,接口字段默認(rèn)是public,static和final,方法默認(rèn)是public和abstract。

如下圖所示,一個(gè)類擴(kuò)展了另一個(gè)類,一個(gè)接口擴(kuò)展了另一個(gè)接口,一個(gè)類實(shí)現(xiàn)了一個(gè)接口。
Java接口示例
在這個(gè)例子中,Printable接口只有一個(gè)方法,它的實(shí)現(xiàn)是在A類中提供的。
interface printable {
void print();
}
class A6 implements printable {
public void print() {
System.out.println("Hello, Interface");
}
public static void main(String args[]) {
A6 obj = new A6();
obj.print();
}
}
上面代碼執(zhí)行結(jié)果如下 -
Hello, Interface
Java接口示例:Drawable
在這個(gè)例子中,Drawable接口只有一個(gè)方法。 它的實(shí)現(xiàn)由Rectangle和Circle類提供。 在實(shí)際情況下,接口由一些開發(fā)者定義,但實(shí)現(xiàn)由不同的實(shí)現(xiàn)提供者提供。 并且它被別人使用。實(shí)現(xiàn)部分被使用接口的用戶隱藏。
創(chuàng)建一個(gè) Java 文件:TestInterface1.java,其代碼如下 -
//Interface declaration: by first user
interface Drawable {
void draw();
}
// Implementation: by second user
class Rectangle implements Drawable {
public void draw() {
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable {
public void draw() {
System.out.println("drawing circle");
}
}
// Using interface: by third user
class TestInterface1 {
public static void main(String args[]) {
Drawable d = new Circle();// In real scenario, object is provided by
// method e.g. getDrawable()
d.draw();
}
}
執(zhí)行上面代碼得到以下結(jié)果 -
drawing circle
Java接口示例:Bank
讓我們來看看另一個(gè)提供了Bank接口實(shí)現(xiàn)的java接口的例子。創(chuàng)建一個(gè) Java 文件:TestInterface2.java,其代碼如下 -
interface Bank {
float rateOfInterest();
}
class SBI implements Bank {
public float rateOfInterest() {
return 9.15f;
}
}
class PNB implements Bank {
public float rateOfInterest() {
return 9.7f;
}
}
class TestInterface2 {
public static void main(String[] args) {
Bank b = new SBI();
System.out.println("ROI: " + b.rateOfInterest());
}
}
執(zhí)行上面代碼得到以下結(jié)果 -
ROI: 9.15
如果一個(gè)類實(shí)現(xiàn)了多個(gè)接口,或者一個(gè)接口擴(kuò)展了多個(gè)接口,即被稱為多重繼承。

參考以下示例代碼的實(shí)現(xiàn) -
interface Printable {
void print();
}
interface Showable {
void show();
}
class A7 implements Printable, Showable {
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("Welcome");
}
public static void main(String args[]) {
A7 obj = new A7();
obj.print();
obj.show();
}
}
執(zhí)行上面代碼得到以下結(jié)果 -
Hello
Welcome
問題:在java中不支持通過類實(shí)現(xiàn)多繼承,但可通過接口實(shí)現(xiàn),為什么?
正如在繼承章節(jié)中所解釋的,在類中不支持多重繼承是因?yàn)槟:?但是在接口的情況下可以支持,因?yàn)榻涌跊]有歧義,接口的具體實(shí)現(xiàn)由實(shí)現(xiàn)類提供。 例如:
interface Printable {
void print();
}
interface Showable {
void print();
}
class TestTnterface3 implements Printable, Showable {
public void print() {
System.out.println("Hello");
}
public static void main(String args[]) {
TestTnterface1 obj = new TestTnterface1();
obj.print();
}
}
上面代碼執(zhí)行得到以下結(jié)果 -
Hello
從上面的例子可以看到,Printable和Showable接口有相同的方法,但它的實(shí)現(xiàn)是由TestTnterface1類提供的,所以沒有歧義。
類可以實(shí)現(xiàn)多個(gè)接口,但接口也擴(kuò)展另一個(gè)接口。
interface Printable {
void print();
}
interface Showable extends Printable {
void show();
}
class TestInterface4 implements Showable {
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("Welcome");
}
public static void main(String args[]) {
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
執(zhí)行上面代碼,得到如下結(jié)果 -
Hello
Welcome
從Java 8開始,我們可以在接口中有方法體。 但需要使其為默認(rèn)方法?,F(xiàn)在來看看一個(gè)例子,創(chuàng)建一個(gè)Java文件:TestInterfaceDefault.java,其代碼如下 -
interface Drawable {
void draw();
default void msg() {
System.out.println("default method");
}
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("drawing rectangle");
}
}
class TestInterfaceDefault {
public static void main(String args[]) {
Drawable d = new Rectangle();
d.draw();
d.msg();
}
}
執(zhí)行上面代碼,得到如下結(jié)果 -
drawing rectangle
default method
從Java 8開始,我們可以在接口中有靜態(tài)方法。 讓我們來看看下面的一個(gè)例子,創(chuàng)建一個(gè)Java文件:TestInterfaceStatic.java 其代碼如下:
interface Drawable {
void draw();
static int cube(int x) {
return x * x * x;
}
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("drawing rectangle");
}
}
class TestInterfaceStatic {
public static void main(String args[]) {
Drawable d = new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}
}
執(zhí)行上面代碼,得到如下結(jié)果 -
drawing rectangle
27
問題:什么是標(biāo)記或標(biāo)記的接口?
沒有成員的接口(僅定義一個(gè)空的接口)稱為標(biāo)記或標(biāo)記接口。 例如:可序列化,可克隆,遠(yuǎn)程等。它們用于向JVM提供一些基本信息,以便JVM可以執(zhí)行一些有用的操作。
//How Serializable interface is written?
public interface Serializable{
}
Java嵌套接口
注意: 一個(gè)接口中可以有另一個(gè)接口,即被稱為嵌套接口。有關(guān)于這方面的知識,將在嵌套類的章節(jié)中詳細(xì)學(xué)習(xí)。 例如:
interface printable {
void print();
interface MessagePrintable {
void msg();
}
}