Is an interface a variable?

Asked

Viewed 709 times

6

I have some questions about interfaces regarding 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 like a contract of the classes that implement it. In the example below is shown the interface Nodelist being used as a variable to store another interface of the same type that is returned by the method getElementsByTagName():

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 type variable? The interface can be instantiated as a class with the command new or not since it can be used as a variable?

  • There are some confusions there, I’ll try to explain, but I don’t even know where to start yet. Exactly what is returned by this method?

  • From what I understood from the code, the purpose has still remained, will only accept if the data type passed to noList is "signed" the contract, ie if implement NodeList. You are restricting that all data passed to this variable, regardless of type, meet what was displayed in the interface NodeList. In the end, the purpose is the same.

  • If I understand correctly, when you do NodeList noList = algo ... are you saying that noList receives something is a super type. Remember that vc can specify a super type(interface or abstract class usually) in method parameters.

3 answers

9


An interface is never a variable, interface is interface, if you want to generalize the term, it is a guy since, yes, it indicates a contract. In Java 8 it even allows to have something beyond the contract, but I imagine that is not the case here.

It is possible declare a variable being of a kind interface no problem, but concretely you can only assign a concrete type, i.e., a class (in other languages, or newer version of Java, can even do other things). Obviously the class that is used to instantiate a concrete object needs to be compatible with the declared interface, so be a type that implements the interface.

So this method used there should return a concrete object that is compliant with the interface NodeList.

In the case of the variable noList can only access members present in the interface (which the object certainly has, by the conformity established in the class). To access other members of the concrete object, just making one cast in the object. The interface has no knowledge about the other members.

It may be that the declared return of the method is 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, shall be another which complies with NodeList (derived from her).

Imagining that this would be the method documenting then the type returned is NodeList. Signature of it:

NodeList getElementsByTagName(String name)

There lists the class IOMetadataNode as a class that implements it. So possibly the method getElementsByTagName() returns a concrete object of this class. But it can be something else, nothing guarantees it, it can even change a day and that "is not your concern" (the use of the interface says this).

How do I know which is the concrete object that returns from the class if it is not in the documentation? I have to look at the sources of this method?

In fact the idea of using the interface is precisely not need to think about the concrete type. It is not your interest to know, use the interface and ready. In many rare cases knowing the type will be useful and will probably do some gambiarra with this information. In general we program for the interface and not for the implementation.

8

TL;DR;

Not. One 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

is referring to a guy.

A small example, if you still confuse the concepts.

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

You see, an interface can define the type of a variable, what happens is that interfaces cannot be instantiated. This means that, the interface may be on the left side of the declaration, but never on the right side, on the right side you always use the concrete type.

Ex:

MinhaInterface var = new MinhaClasseQueImplementaMinhaInterface();

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

I won’t go into detail about how interfaces work because we already have good answers about it here. See:

In object orientation, why interfaces are useful?
Abstract Class X Interface
In OOP, an interface can have attributes?
When to use Inheritance, Abstract Class, Interface or a Trait?

3

In the code:

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

NodeList is the type, in this case an interface.

noList is a variable of the type NodeList.

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

String texto;

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

What I wrote may seem very obvious, but your question is a little mixed up and it’s hard to know exactly where your doubt is.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.