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

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

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

18条回答
  •  半阙折子戏
    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

提交回复
热议问题