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

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

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

相关标签:
18条回答
  • 2020-11-22 17:16
    • A extends B:

      A and B are both classes or both interfaces

    • A implements B

      A is a class and B is an interface

    • The remaining case where A is an interface and B is a class is not legal in Java.

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

    Both keywords are used when creating your own new class in the Java language.

    Difference: implements means you are using the elements of a Java Interface in your class. extends means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.

    Refer to oracle documentation page on interface for more details.

    This can help to clarify what an interface is, and the conventions around using them.

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

    In the most simple terms extends is used to inherit from a class and implements is used to apply an interface in your class

    extends:

    public class Bicycle {
        //properties and methods
    }
    public class MountainBike extends Bicycle {
        //new properties and methods
    }
    

    implements:

    public interface Relatable {
        //stuff you want to put
    }
    public class RectanglePlus implements Relatable {
        //your class code
    }
    

    if you still have confusion read this: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html

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

    extends is for when you're inheriting from a base class (i.e. extending its functionality).

    implements is for when you're implementing an interface.

    Here is a good place to start: Interfaces and Inheritance.

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

    Classes and Interfaces are both contracts. They provide methods and properties other parts of an application relies on.

    You define an interface when you are not interested in the implementation details of this contract. The only thing to care about is that the contract (the interface) exists.

    In this case you leave it up to the class which implements the interface to care about the details how the contract is fulfilled. Only classes can implement interfaces.

    extends is used when you would like to replace details of an existing contract. This way you replace one way to fulfill a contract with a different way. Classes can extend other classes, and interfaces can extend other interfaces.

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

    extends is for extending a class.

    implements is for implementing an interface

    The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much).

    Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces.

     public interface ExampleInterface {
        public void doAction();
        public String doThis(int number);
     }
    
     public class sub implements ExampleInterface {
         public void doAction() {
           //specify what must happen
         }
    
         public String doThis(int number) {
           //specfiy what must happen
         }
     }
    

    now extending a class

     public class SuperClass {
        public int getNb() {
             //specify what must happen
            return 1;
         }
    
         public int getNb2() {
             //specify what must happen
            return 2;
         }
     }
    
     public class SubClass extends SuperClass {
          //you can override the implementation
          @Override
          public int getNb2() {
            return 3;
         }
     }
    

    in this case

      Subclass s = new SubClass();
      s.getNb(); //returns 1
      s.getNb2(); //returns 3
    
      SuperClass sup = new SuperClass();
      sup.getNb(); //returns 1
      sup.getNb2(); //returns 2
    

    Also, note that an @Override tag is not required for implementing an interface, as there is nothing in the original interface methods to be overridden

    I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming

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