Why is an interface or an abstract class useful? (or for what?)

前端 未结 2 1833
闹比i
闹比i 2020-12-21 07:28

So my question is, why to use interfaces or abstract classes? Why are they useful, and for what?

Where can i use them intelligently?

2条回答
  •  长情又很酷
    2020-12-21 07:51

    Interfaces allow you to express what a type does without worrying about how it is done. Implementation can be changed at will without impacting clients.

    Abstract classes are like interfaces except they allow you to provide sensible default behavior for methods where it exists.

    Use and examples depend on the language. If you know Java, you can find examples of both interfaces and abstract classes throughout the API. The java.util collections have plenty of both.

提交回复
热议问题