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

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

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

18条回答
  •  [愿得一人]
    2020-11-22 17:10

    When a subclass extends a class, it allows the subclass to inherit (reuse) and override code defined in the supertype. When a class implements an interface, it allows an object created from the class to be used in any context that expects a value of the interface.

    The real catch here is that while we are implementing anything it simply means we are using those methods as it is. There is no scope for change in their values and return types.

    But when we are extending anything then it becomes an extension of your class. You can change it, use it, reuse use it and it does not necessarily need to return the same values as it does in superclass.

提交回复
热议问题