Implements vs extends: When to use? What's the difference?

前端 未结 18 751
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 17:03

Please explain in an easy to understand language or a link to some article.

18条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 17:25

    Implements is used for Interfaces and extends is used to extend a class.

    To make it more clearer in easier terms,an interface is like it sound - an interface - a model, that you need to apply,follow, along with your ideas to it.

    Extend is used for classes,here,you are extending something that already exists by adding more functionality to it.

    A few more notes:

    an interface can extend another interface.

    And when you need to choose between implementing an interface or extending a class for a particular scenario, go for implementing an interface. Because a class can implement multiple interfaces but extend only one class.

提交回复
热议问题