Interface vs Base class

后端 未结 30 2537
甜味超标
甜味超标 2020-11-21 07:34

When should I use an interface and when should I use a base class?

Should it always be an interface if I don\'t want to actually define a base implementation of the

30条回答
  •  余生分开走
    2020-11-21 07:37

    Don't use a base class unless you know what it means, and that it applies in this case. If it applies, use it, otherwise, use interfaces. But note the answer about small interfaces.

    Public Inheritance is overused in OOD and expresses a lot more than most developers realize or are willing to live up to. See the Liskov Substitutablity Principle

    In short, if A "is a" B then A requires no more than B and delivers no less than B, for every method it exposes.

提交回复
热议问题