# Interface与abstract类的区别,应用场景.

接口中只能存在`抽象方法`

一个类能实现`多个接口`,但只能实现`一个抽象类`

接口中的方法修饰符默认是public，而且只能是public

抽象方法可以有public、protected和default这些修饰符，不能是private的

抽象类中可以存在`非`抽象方法

抽象是`对类`的抽象，是一种`模板设计`，而`接口`是对`行为`的抽象，是一种`行为的规范`。

## interface的应用场合

```
A. 类与类之前需要特定的接口进行协调，而不在乎其如何实现。
B. 作为能够实现特定功能的标识存在，也可以是什么接口方法都没有的纯粹标识。
C. 需要将一组类视为单一的类，而调用者只通过接口来与这组类发生联系。
D. 需要实现特定的多项功能，而这些功能之间可能完全没有任何联系。
```

## abstract class的应用场合

```
一句话，在既需要统一的接口，又需要实例变量或缺省的方法的情况下，就可以使用它。最常见的有：
1. 定义了一组接口，但又不想强迫每个实现类都必须实现所有的接口。
可以用abstract class定义一组方法体，甚至可以是空方法体，
然后由子类选择自己所感兴趣的方法来覆盖。
2. 某些场合下，只靠纯粹的接口不能满足类与类之间的协调，还必需类中表示状态的变量来区别不同的关系。
abstract的中介作用可以很好地满足这一点。
3. 规范了一组相互协调的方法，其中一些方法是共同的，与状态无关的，可以共享的，
无需子类分别实现；而另一些方法却需要各个子类根据自己特定的状态来实现特定的功能
```


---

# 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://lxs-shmily.gitbook.io/java-notes/citicbank/citic-summary/18.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.
