When to use an interface instead of an abstract class and vice versa?

前端 未结 23 2493
忘了有多久
忘了有多久 2020-11-22 04:29

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage.

When wou

23条回答
  •  醉梦人生
    2020-11-22 05:19

    For me, I would go with interfaces in many cases. But I prefer abstract classes in some cases.

    Classes in OO generaly refers to implementation. I use abstract classes when I want to force some implementation details to the childs else I go with interfaces.

    Of course, abstract classes are useful not only in forcing implementation but also in sharing some specific details among many related classes.

提交回复
热议问题