接口是常量值和方法定义的集合。接口是一种特殊的抽象类。

java类是单继承的。classB Extends classA

java接口可以多继承。Interface3 Extends Interface0, Interface1, interface……

以下是spring ApplicationContext 接口的代码,同时继承了多个接口

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,MessageSource, ApplicationEventPublisher, ResourcePatternResolver { /*** Return the unique id of this application context.* @return the unique id of the context, or {@code null} if none*/String getId(); /*** Return a name for the deployed application that this context belongs to.* @return a name for the deployed application, or the empty String by default*/String getApplicationName(); /*** Return a friendly name for this context.* @return a display name for this context (never {@code null})*/String getDisplayName(); /*** Return the timestamp when this context was first loaded.* @return the timestamp (ms) when this context was first loaded*/long getStartupDate(); /*** Return the parent context, or {@code null} if there is no parent* and this is the root of the context hierarchy.* @return the parent context, or {@code null} if there is no parent*/ApplicationContext getParent(); /*** Expose AutowireCapableBeanFactory functionality for this context.* <p>This is not typically used by application code, except for the purpose of* initializing bean instances that live outside of the application context,* applying the Spring bean lifecycle (fully or partly) to them.* <p>Alternatively, the internal BeanFactory exposed by the* {@link ConfigurableApplicationContext} interface offers access to the* {@link AutowireCapableBeanFactory} interface too. The present method mainly* serves as a convenient, specific facility on the ApplicationContext interface.* <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException* after the application context has been closed.</b> In current Spring Framework* versions, only refreshable application contexts behave that way; as of 4.2,* all application context implementations will be required to comply.* @return the AutowireCapableBeanFactory for this context* @throws IllegalStateException if the context does not support the* {@link AutowireCapableBeanFactory} interface, or does not hold an* autowire-capable bean factory yet (e.g. if {@code refresh()} has* never been called), or if the context has been closed already* @see ConfigurableApplicationContext#refresh()* @see ConfigurableApplicationContext#getBeanFactory()*/AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}

不允许类多重继承的主要原因是,如果A同时继承B和C,而B和C同时有一个D方法,A如何决定该继承那一个呢?

但接口不存在这样的问题,接口全都是抽象方法继承谁都无所谓,所以接口可以继承多个接口。

注意:

1.一个类如果实现了一个接口,则要实现该接口的所有方法。

2.方法的名字、返回类型、参数必须与接口中完全一致。如果方法的返回类型不是void,则方法体必须至少有一条return语句。

3.因为接口的方法默认是public类型的,所以在实现的时候一定要用public来修饰(否则默认为protected类型,缩小了方法的使用范围)。

Java中的接口可以多继承相关推荐

  1. Java中实现接口与继承的区别

    ** Java中实现接口与继承的区别 ** 首先,先来了解一下什么是接口和继承.接口一般是使用interface来定义的.接口定义同类的定义类似,分为接口的声明和接口体,其中接口体由常量定义和方法定义 ...

  2. Objective-C 入门(七)协议 protocol(JAVA中的接口)

    Objective-C 入门(七)协议 protocol(JAVA中的接口) 接口的作用想必大家都比较了解 OV中的 protocol 相比接口作用相似 语法稍有不同 1.先来看声明一个协议 在创建文 ...

  3. java中接口什么时候用_我什么时候应该在java中使用接口?

    在Java中精确使用接口的一个很好的例子将是理想的,适用于任何特定的规则. 看看丹以前所有的问题,他似乎只是逐字逐句地张贴家庭作业/考试问题. 令人惊讶的是,这些不是考试题或其他-今天才找到这个网站, ...

  4. java的接口怎么理解_如何理解Java 中的接口

    一.接口(Interface) 在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法.接口并不是类,编写接口的方 ...

  5. java中 以下接口以键_java复习题

    1.Java中的long类型占用()个字节. A.1 B.2 C.4 D.8 2.以下关于继承的叙述不正确的是(). A.在Java中类只允许单一继承 B.在Java中一个类只能实现一个接口 C.在J ...

  6. java中的接口的定义以及实现关系

    一.什么是接口? - 硬件接口:设备之间的连接方式 - 软件接口:程序代码,特殊的类,表示一种规范,是具有N个方法的特征集合. java中的接口: 专家说的多个抽象类的抽象就是接口. interfac ...

  7. java中的接口是什么?

    接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口 ...

  8. 第32节:Java中-构造函数,静态方法,继承,封装,多态,包

    构造函数实例 class Cat{// 设置私有的属性 name private String name;// 设置name的方法public void setName(String Name) {t ...

  9. Java中实现接口的三种方式您造吗?

    本文介绍了Java中实现接口的三种方式:常规实现方式.匿名内部类和 Lambda表达式实现方式.希望已经了解的同学可以重新温习一下,不了解的同学则从中受益! Java中接口最常规的实现方式 同学们都会 ...

最新文章

  1. python 为什么用%格式化_Python中应该使用%还是format来格式化字符串?
  2. HDOJ 2012 素数判定 解题报告
  3. headerIP php_PHP使用curl伪造IP地址和header信息
  4. 红旗linux 进不去图形界面,进不了红旗Linux6.0的图形界面请高手帮忙
  5. anaconda安装shapefile_Anaconda3详细安装使用教程及问题总结
  6. iOS 15 如何让 App 启动更快?
  7. 第一章节 初始类和对象
  8. 旋风加速安卓Android官网,旋风加速官网版最新版
  9. Scrum敏捷开发流程
  10. 智慧环保监测系统提升环境监管水平
  11. 阿里20亿美金收购考拉,丁磊到底是怎么“失身”的?
  12. 点击“安全删除硬件并弹出媒体”不显示可删除移动设备
  13. 心脑血管应该注意那些?
  14. iVMS-4200 Vs区别_【5G科普】5G知识知多少?#之5G与4G的区别
  15. mac 批量文件编码格式转换
  16. 【NOIP2014】提高组初赛答案加解析
  17. php天花板好吗,最近听了听平头塞天花板瑞弦D3,说说感受(对比钛铃)
  18. C# 调用 速印标签标准版 的模板lbl文件
  19. 大学自学python可以考哪些证_大学生:大学期间可以考哪些证?
  20. MQTT客户端(基于mosquitto库)上报温度到腾讯云

热门文章

  1. mac运行windows应用:Parallels Desktop Mac版本
  2. 使用虚拟机创建本地服务器
  3. 手机RPG首创鸿文 混沌之戒移植安卓平台
  4. 拼多多双十一活动怎么取消?怎么做好运营?
  5. 前端实现跨域的三种方式
  6. win7 docker centos安装mysql_win7下docker环境centos容器中安装mysql5.7
  7. 怎样恢复删除的视频?这三种方法真的很管用!
  8. ZROI 2021 10联day8 T1 题(期望+二维区间DP)
  9. Linux根目录下的内容
  10. 什么是微商城?微商城与微信之间的关系