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

前端 未结 23 2689
忘了有多久
忘了有多久 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:13

    When to prefer an abstract class over interface?

    1. If one plans on updating a base class throughout the life of a program/project, it is best to allow that the base class be an abstract class
    2. If one is trying to build a backbone for objects that are closely related in a hierarchy, it is highly beneficial to use an abstract class

    When to prefer an interface over abstract class?

    1. If one is not dealing with a massive hierarchical type of framework, interfaces would be a great choice
    2. Because multiple inheritance is not supported with abstract classes(diamond problem), interfaces can save the day

提交回复
热议问题