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,
[ Edited: ] Terminology:
• superclass, base class, parent class
• subclass, derived class, child class. Also called an 'extended' class in Java.
[ Implements vs extends: ]
When to use? What's 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'.
In Java, a class can extend maximum one class only.
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. 😉
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.
I believe you are correct in what I should have said regarding "inherited classes."
Thanks for pointing that out. 🙏