[Solved] What is the difference between “extends” and “implements” in Java?

8/23/2019 1:47:51 PM

Puzic Elena

23 Answers

New Answer


Puzic Elena "Extending" on the good answers already posted by Jai Shree Krishna 🙏 🙏 🙏 and Airree, I will focus on the reasons for using these specific words. In OOP languages, a class can inherit all the public and protected members of another class. These classes are known as subclasses or derived classes. The inherited classes are known as super or base classes. Derived classes are "extending" or are "extensions" of the base classes they inherit. That is... they are adding onto the current capabilities and attributes of the base class. Likewise, a class "implements" an interface because the interface merely defines method signatures expected to be exposed for a given capability. There is no code defined in interfaces. The code / logic for a given interface is left to be "implemented" in the classes "implementing" the interface. Interfaces are a contract or binding agreement of sorts for different classes to guarantee they will "implement" their own logic for a given set of methods.


~ swim ~ Indeed... an interface can "extend" one or more interfaces. Thanks for pointing that out. 😉 The cool thing is, the usage of the word "extends" continues to be consistent with my earlier explanation in that the derived interface will inherit the members of the other interfaces. Classes that "implement" the derived interface will be required to "implement" all members of all interfaces in the interface hierarchy.


Puzic Elena I know that David Carroll 🙌explained these concepts really well,👍 but here's how I learned the Inheritance Concept ( One class is 'derived' from another, inherits all its data and methods ), and reinforced the basics with my friend D_Stark 🍻. **In Java, 'every' class is part of an inheritance hierarchy, because the class called Object ( java.lang.Object ) is automatically a base class to every Java class. • In Java we use the key 'extends' to inherit from a superclass, https://code.sololearn.com/c8Aakq7iZ9ai/?ref=app [ Edited: ] Terminology: • superclass, base class, parent class • subclass, derived class, child class. Also called an 'extended' class in Java.


Sonic the super/base classes are inherited by the derived classes so David Carroll is right when he refers to them as inherited classes. Yeah, English can be pretty terrible.


[ Implements vs extends: ] When to use? What's the difference? • https://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference **The ability to add functionality to a class by subclassing, or extending, it is central to the object-oriented programming paradigm.


• Implementing an Interface Just as a class uses 'extends' to specify its superclass, it can use 'implements' to name one or more interfaces it supports. 'implements' is a Java keyword that can appear in a class declaration following the 'extends' clause. 'implements' should be followed by the name or names of the interface(s) the class implements, with multiple names separated by commas. • When a class declares an interface in its 'implements' clause, it is saying that it provides an implementation (i.e., a body) for each method of that interface. If a class implements an interface but does not provide an implementation for every interface method, it inherits those unimplemented 'abstract' methods from the interface and must itself be declared 'abstract'. If a class implements more than one interface, it must implement every method of each interface it implements (or be declared 'abstract' ). /* "Java in a Nutshell" 3th ed. */


In simple words, 'extends' is used for extending a class and 'implements 'is used for implementing the interfaces. It’s the main 'difference between extends vs implements'. Note: In Java, a class can extend maximum one class only.


Puzic Elena 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.


David Carroll like always 🔝 thank you a lot for this explanation 👏🏼👍🏼


Puzic Elena As always... my pleasure! I tend to explore concepts by trying to understand why certain keywords might have been used. This approach has helped me quickly grasp and retain abstract concepts throughout my career and is probably the reason I'm so verbose in my responses. 😮 I hope this helps with your approach to learning. 😉


extends is used for class inheritance; implements is used for interface implementation


David Carroll are you sure you meant to say that the inherited classes are known as super or base classes? Or did you mean that the classes inherited from are called that? Sorry my semantics/English are/is not perfect but I thought that the inherited class IS the derived or sub class. Edit: Thanks Jamie💐. Maybe this is just my confusion about apparent inconsistencies in English. If you derive something from a base class, you're the derived class but if you inherit something from a base class you're not the inherited class.


Sonic You might have broken my mind for a few days. 🤯🤣 I didn't want to pollute this thread with the ramblings of a madman... so I responded to you in this post. https://www.sololearn.com/post/139826/ I believe you are correct in what I should have said regarding "inherited classes." Thanks for pointing that out. 🙏


David Carroll In Java an interface can extend another interface.


Jai Shree Krishna 🙏 🙏 🙏 thank you for explanation


Airree thank you for explanation


David Carroll I had no doubt about your explanation. You just forgot to mention that.


~ swim ~ I rephrased my response in case it sounded like anything other than a clarification to those following the thread. 👌


Extends use for class to class. Interface to interface Implements use for class to interface.


Whenever we know about method use interface Else abstract