# 認識介面

介面（interface）具有資料成員 與 抽象函數；那麼 他與「抽象類別」有何不同之處？

* 介面中的資料成員必須設定初值
* 介面沒有一般函數，只有抽象函數

進入範例之前，先了解一下 介面 的格式：

```
interface 介面名稱
{
  final 資料型態 成員名稱=常數;

  public abstract 傳回值資料型態 函數名稱(引數...);
  //抽象函數並無定義處理方式
}
```

因為介面只有抽象函數，abstract 的關鍵字可以省略；\
而資料成員部分：因為此值不會被更改，final 也是可以省略的。

抽象函數的修飾子也只能使用公有 public (或不宣告)，\
為了都能取用到，所以不能宣告 私有private或保護 protected 。

再次強調一次重點：

* 介面中的資料成員必須設定初值
* 介面沒有一般函數，只有抽象函數

知道介面只含有抽象函數，不禁也讓我們聯想到：\
抽象函數只定義其名稱，而沒有處理方式，因此介面跟抽象類別一樣無法直接建立物件。

Ｑ：那麼我們該如何透過定義好的介面來造一個類別，並透過之來建立物件？

Ａ：使用 介面 a 來打造 類別 b ，我們稱為介面的實作（implementation）、以類別b實作介面a

介面的實作（implementation）格式也很簡單：

```
class 類別名稱 implements 介面名稱
{
   ...
}
```

下一篇將透過完整範例做更加詳盡的解說，加深對介面實作 的使用。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java.4-x.tw/java-13/java-13-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
