返回信息流“接口是特殊的抽象类”这句话对吗?
这是一条镜像帖。来源:北邮人论坛 / java / #49809同步于 2016/4/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
[问题]接口和抽象类
dongqing
2016/4/28镜像同步46 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
这种概念性问题,个人觉得最好是定义入手。
Oracle关于Interface的定义
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
1. “In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types”
Oracle关于Abstract Class的定义:
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
2. “An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.”
3.Abstract Classes Compared to Interfaces
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
从上面的三个内容,首先,Interface不是一种特殊的Abstract Class。
但是你要说“接口是一种特殊的抽象类”,这个对不对,看你怎么定义这个抽象类了,是Java里Abstract Class的翻译,还是指的面向对象里的抽象类。
“抽象类往往用来表征对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。”
如果是前者,答案应该是否,也就是前面有同学提到的,“接口区别于抽象类在于多继承,但是接口的定义类似于抽象类”
如果是后者,答案应该是是,个人理解为,面向对象里只要是对具体抽象的类都可以理解为抽象类。