Is an interface a variable?

5

I have some doubts about interfaces in relation to the way they are used.

I thought the interface was not used as a variable that represents and stores some kind of data, see MinhaInterface mInterface; instead of having the sole purpose of being as a contract of the classes that implement it. The example below shows the NodeList interface being used as a variable to store another interface of the same type that is returned by the getElementsByTagName() method:

NodeList noList = document.getElementsByTagName("minhaTag");

So, how can an interface be a variable and what is the difference of the interface type variable compared to the class variable? Can the interface be instantiated as a class with the new command or not since it can be used as a variable?

    
asked by anonymous 01.07.2016 / 20:35

3 answers

9

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 .

    
01.07.2016 / 20:49
7

TL; DR;

No . One thing has nothing to do with the other.

You're confusing some things.

First, in the section below, when you refer to "variable"

  

I thought the interface was not used as a variable that represents and stores some kind of data

You want to refer to a type .

A small example if you still confuse the concepts.

Tipo variavel = new Tipo();
//Ou como no caso de usar interface
Interface variavel = new TipoQueImplementaInterface();

Well, a interface can set the type of a variable, what happens is that interfaces can not be instantiated . This means that the interface can be on the left side of the statement, but never on the right , on the right you always use the concrete type.

Ex:

MinhaInterface var = new MinhaClasseQueImplementaMinhaInterface();

This basically, and among other things, allows the variable var to receive any type that implements MinhaInterface without having to worry about implementing the methods defined in the interface.

I will not go into detail about how interfaces work because we already have good answers about it here. See:

In object orientation, why are interfaces useful?
Abstract Class X Interface
In OOP , an interface can have attributes?
When should I use Inherit, Abstract Class, Interface, or a Trait? / a>

    
01.07.2016 / 20:57
3

In the code:

NodeList noList = document.getElementsByTagName("minhaTag");

NodeList is the type, in this case an interface.

noList is a variable of type NodeList .

The fact that you declare noList as a variable of type NodeList does not imply that the NodeList interface is now a variable. They are different things. The same goes when declaring a variable as String , see:

String texto;

The fact that you declare text as String , does not make String a variable. Therefore, NodeList and String are types, regardless of whether they are concrete, abstract classes, interfaces or enums.

What I wrote may seem very obvious, but your question is a bit mixed and it's difficult to know for sure where your question is.

What should be clear is that the document.getElementsByTagName("minhaTag"); call must return an object of a particular concrete class that implements the NodeList interface.

    
01.07.2016 / 20:59