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

前端 未结 18 598
爱一瞬间的悲伤
爱一瞬间的悲伤 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

    Extends is used when you want attributes of parent class/interface in your child class/interface and implements is used when you want attributes of an interface in your class.

    Example:

    1. Extends using class

      class Parent{

      }

      class Child extends Parent{

      }

    2. Extends using interface

      interface Parent{

      }

      interface Child extends Parent{

      }

    3. Implements

    interface A{

    }

    class B implements A{

    }

    Combination of extends and implements

    interface A{
    
    }
    
    class B
    
    {
    
    }
    
    class C implements A,extends B{
    
    }
    

提交回复
热议问题