Class | Abstract Class | Interface | |
Instantiated | ✔ | X | X |
Extend/Implement Multiple | X | X | ✔ |
Contract | X | ✔ | ✔ |
Implementation | ✔ | ✔ | X |
Single Place Versioning | ✔ | ✔ | X |
All Methods Public (Access Modifiers) | X | X | ✔ |
Properties | ✔ | ✔ | X |
Constants | ✔ | ✔ | ✔ |
Overridable Constant | ✔ | ✔ | X |
Type Checked | ✔ | ✔ | ✔ |
Abstract Class
When determining if an abstract class is right for your situation, you should consider the
following:
- Do you want to share code among several closely related classes?
- Do you expect classes that extend your abstract class will have many common methods or properties?
- Do you require access modifiers other than public (such as protected and private)?
Interface
You should consider using interfaces if any of these statements apply to your situation:
- You expect that unrelated classes would implement your interface. For example, the interfaces
savoryFlavor and Countable, are implemented by many unrelated classes. - You want to specify the behavior of a particular data
type, but are not concerned about how that behavior is implemented. - You want to take advantage of multiple
inheritance of type.