An interface is never a variable, interface is interface, if you want to generalize the term, it is a type of data that, yes, indicates a contract. In Java 8 it even allows you to have something beyond the contract, but I imagine that is not the case here.
You can declare a variable being of type interface
with no problem whatsoever, but concretely you can only assign a concrete type , that is, a class (in other languages, maybe other things). Obviously, the class that is used to instantiate a concrete object must be compatible with the declared interface.
Then this method should return a concrete object that conforms to the NodeList
interface.
In this case, the noList
variable can only access the members present in the interface (which the object certainly has, due to the compliance established in the class). To access other members of the concrete object, just cast cast . The interface is not aware of other members.
The declared return of the method may be an interface, but the actual return will be a concrete object that conforms to the interface of its return. Obviously, if this interface is not NodeList
, it should be one that conforms to NodeList
(derived from it).
Imagining this would be the documenting method then the returned type is NodeList
. Signature of it:
NodeList getElementsByTagName(String name)
There list the IIOMetadataNode
class as a class that implements it. Then possibly the getElementsByTagName()
method returns a concrete object of this class. But it can be something else, nothing guarantees this, it can even change one day and this "is not your business" (use of the interface says this).
How do I know what is the concrete object that returns from the class if it does not have documentation? Do I have to look at the sources of this method?
Actually the idea of using the interface is precisely not having to think about the concrete type. It is not in your interest to know, use the interface and that's it. In many rare cases knowing the type will be useful and will probably do some gambiarra with this information. In general we programmed for the interface and not for the implementation .