Class vs. Interface

后端 未结 10 1495
孤城傲影
孤城傲影 2020-12-10 03:18

I have a quite basic question:

When should we decide to use Interface or Class for a specific class?

For example: says we have 2 classes, Customer and Doctor

10条回答
  •  孤街浪徒
    2020-12-10 03:45

    Simply put, you use classes when there is code/implementation involved and interfaces when it is just interface descriptions. When referring to objects in your code, prefer to refer to the interfaces as it makes it easier to replace the actual implementation (or add more and different implementations).

    And yes, both Docor and Customer/Patient are likely to implement or extend Person.

提交回复
热议问题