# 認識類別的繼承

繼承可說是類別的精華，有程式碼再利用的概念。

> 繼承：可保有原先的程式碼，並加以擴增；再利用的程式碼可以重複使用。

每個類別只能有一個父類別，而父類別可擁有一個以上的子類別。

![](/files/-MPaEpMASwOJ64SjcIEg)

子類別除了自己的成員外，也包含父類別的幾樣成員是可以直接繼承的。

父類別與子類別 能繼承之關係圖如下：

![](/files/-MPaEu6IQelN8FeL4f11)

其中較特殊的如 private 成員，必續透過父類別的函數才可以做存取。

另外 父類別中的 建構元 以及 設為終止繼承的成員也是無法被子類別繼承的！

> *繼承的寫法是使用 extends 關鍵字，將父類別繼承給子類別。*\
> \&#xNAN;*做法很簡單，先寫子類別名稱 再加extends 關鍵字， 後方再填上父類別名稱 即可！*
>
> ```
> class 父類別
> {
>   ...
> } 
>
> class 子類別 extends 父類別
> {
>  ...
> }
> ```

於下篇文章，將會帶入範例進一步了解繼承的使用。


---

# 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-11/java-11-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.
