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

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

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

相关标签:
18条回答
  • 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.

    0 讨论(0)
  • 2020-11-22 17:27

    A class can only "implement" an interface. A class only "extends" a class. Likewise, an interface can extend another interface.

    A class can only extend one other class. A class can implement several interfaces.

    If instead you are more interested in knowing when to use abstract classes and interfaces, refer to this thread: Interface vs Abstract Class (general OO)

    0 讨论(0)
  • 2020-11-22 17:28

    Extends : This is used to get attributes of a parent class into base class and may contain already defined methods that can be overridden in the child class.

    Implements : This is used to implement an interface (parent class with functions signatures only but not their definitions) by defining it in the child class.

    There is one special condition: "What if I want a new Interface to be the child of an existing interface?". In the above condition, the child interface extends the parent interface.

    0 讨论(0)
  • 2020-11-22 17:31

    An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.

    An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class). For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.

    0 讨论(0)
  • 2020-11-22 17:31

    As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.

    For more details

    0 讨论(0)
  • 2020-11-22 17:33

    Those two keywords are directly attached with Inheritance it is a core concept of OOP. When we inherit some class to another class we can use extends but when we are going to inherit some interfaces to our class we can't use extends we should use implements and we can use extends keyword to inherit interface from another interface.

    0 讨论(0)
提交回复
热议问题