6

java接口(Interface)

 3 years ago
source link: https://blog.csdn.net/qq_44954966/article/details/112681389
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java接口(Interface)

接口是java中提供的一种有别于类的特殊类型。在接口内只能包含抽象方法和常量,从JDK1.8开始,接口中也允许存在默认方法。与类相比而言,接口更像是一种约束形式,行为准则。

具体内容及相关细节:

1.接口使用interface关键字定义
在这里插入图片描述

2.接口不能直接被实例化,需要具体类实现
在这里插入图片描述

3.接口中允许定义抽象方法(可以省略abstract关键字),必须使用public修饰符(public修饰符也可省略),实现类必须重写抽象方法
在这里插入图片描述

4.接口中允许使用default关键字定义默认方法,在实现类中可以选择重写,也可以选择不重写
在这里插入图片描述

5.接口中定义常量,无需static关键字和final关键字(可以缺省,java编译时自动补全)
在这里插入图片描述

6.实现类实现接口时使用implement关键字,并且可以一次实现多个。例如存在IUsb接口,具体类UsbMouse使用implements关键字表示实现了这个接口。
在这里插入图片描述

7.接口可以继承其它接口,相对于类的继承,接口的之间的继承,一个接口一次可继承多个接口。
在这里插入图片描述


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK