Difference between Interface And Abstract Class

This question already has an answer here:

  • Interface vs Abstract Class (general OO) 33 answers
  • What is the difference between an interface and abstract class? 32 answers

  • Interfaces and abstract classes are different in that interfaces describe behavior, while abstract classes define partial implementations. Interfaces have the advantage that they can be implemented by any object which provides the necessary method, regardless of what classes that object inherits from.

    Abstract methods are typically used to provide a partial implementation. An example is the List interface which defines the behavior of List collections, compared to AbstractList which provides some of the methods needed by most List implementations, to make it easier to implement a List. Lists aren't required to inherit from AbstractList, but many implementations do, but code which uses a List never has to care whether the implementation they use extends AbstractList or not.

    Some people use abstract classes as a substitute for interfaces, but this is generally considered an anti-pattern.


    因为,Abstract类可以有非抽象方法,所以你可以在非抽象方法中重用代码!

    链接地址: http://www.djcxy.com/p/54254.html

    上一篇: PHP,OOP界面和抽象

    下一篇: 界面与抽象类的区别