Difference between abstract class with all method abstract and interface?

后端 未结 7 1831
感动是毒
感动是毒 2020-12-29 14:05

I had an interview where interviewer asked me first what is the difference between abstract class with all the methods abstract and an interface.

I replied that if

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 14:16

    I can't say what your interviewer had in mind, but an interface is more of a "contract" whereas an abstract base class, while it can play that role too, is more geared towards hierarchies or IS-A relationships. E.g. an Apple IS-A Fruit, a Pear IS-A Fruit, etc. But you're right, they could well be used interchangeably for practical purposes in that context, but an OO purist might not want to use an abstract class unless they were expressing IS-A relationship(s).

提交回复
热议问题