有关“抽象类”和“接口”的异同之处?

我前两天去听有关《设计模式》的培训,中间有个老师提问“抽象类”和“接口”的区别在哪里,当时好多学生都没有回答上来,这属于oop的问题,老师讲了一会,因为是讲设计模式,所以就没多讲这个问题,请大侠们多多指教

据我理解:
"抽象类"为"abstract class",里面的method可以有实现了的,,
而"接口"为"interface",它比"abstract"更高一层,里面method是纯声明,没有任何实现.而且interface的一个很重要的用途是可以用它来实现c++中的多重继承机制(java是单根继承体系),比如一个derived class只可以extends一个based class却可以implements多个interface......不知我这样说对不对

单纯提这个问题是没有意义的,如果要回答,那就是因为它们的定义不同。

abstract class and interfaces are both language construts
that are designed for different purposes, hence
having different merits.

atstract class has been defined in language C++ already,
while interface was introduced later (in VC?).

In Java, interface can be used to implement multi-inherence
as said in previous email. It should be used to model
behavor and it carries no implementation at all.
Hence it can be implemented by any classes.

Abstract class is used in situations where you have
common behavors shared by multiple sub-classes. Hence
its use is very limited.

两种的内涵是不一样的,抽象类与具体实现是is-a的关系,接口与具体实现则仅表示一种完成一类功能的关系。

提一个有意义的问题,在设计时什么时候使用抽象类,什么时候使用接口?

abstract class 应该是一种预定义了的一种结构,有些内容想到了就实现出来,有些想不到,只有一个概念,那就只列一个概念,比如想象若干年后的太空船,可以定义成一个abstract class,却不能实现出来,除非你知道了做法。

interface 则更多的只是向外界表达一种样子,从这个角度上来讲,它的层次比abstract class要高一层,所以其通用性更好些。