Class vs. Interface

后端 未结 10 1460
孤城傲影
孤城傲影 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:29

    Think of the interface as a contract. The class may commit to the contract (implement the interface)

    Suppose you have class Person with subclasses Doctor and Patient. Then you'd have interface Treatable with the method getSymptoms() implemented by Patient and interface Treating with method cure(Treatable) implemented by Doctor. Most likely cure(Treatable) would call getSymptoms() at some point ...

提交回复
热议问题