Interfaces can be instantiated?

Asked

Viewed 610 times

1

Hello. I know interfaces can’t be instantiated, but I came across a code that doesn’t make much sense to me.

EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();

That second line that got me confused. nameField is an instance of EditText. The method getText() is called by the object. This method returns an object of type Editable, who calls the method toString()? My interpretation is correct?

Why class Editable is an interface. How can it be instantiated in this example? Someone can tell me what is happening?

1 answer

4

The only wrong detail in this example code is that the variable of the first line has the same name as the variable of the second line, which does not compile. These variables would need to have different names. That is, it would have to be like this:

EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();

You cannot instantiate interfaces directly, but you can embed classes that implement it. For example:

public interface Animal {
    public String fazerBarulho();
}

public class Cachorro implements Animal {
    @Override
    public String fazerBarulho() {
        return "au au";
    }
}

public class Gato implements Animal {
    @Override
    public String fazerBarulho() {
        return "miau";
    }
}

public class Teste1 {
    public static void main(String[] args) {

        // O tipo das variáveis é de uma interface,
        // mas o que é instanciado são as classes que a implementam.
        Animal x = new Cachorro();
        Animal y = new Gato();

        System.out.println(x.fazerBarulho()); // Imprime "au au".
        System.out.println(y.fazerBarulho()); // Imprime "miau".
    }
}

Note that the type of variables are interfaces, but what is real instantiation are classes that implement them.

Back to your code, then we have that the variable type of the first line is a EditText (a class). When calling the getText(), an object of the type Editable is provided. The Editable is an interface, but the object obtained is that of some class that implements this interface. In this object of type Editable, the method toString() is invoked, thus providing a String.

Every object has a method called toString() because this method is defined in the Object and like all other classes are subclasses of Object, soon, they will inherit this method and often overwrite it. For example:

public class Pessoa {
    private String nome;
    private int idade;

    public Pessoa(String nome, int idade) {
        this.nome = nome;
        this.idade = idade;
    }

    @Override
    public String toString() {
        return "Meu nome é " + nome + " e tenho " + idade + " anos";
    }
}

public class Pedra {
}

public class Teste2 {
    public static void main(String[] args) {
        Pessoa carlos = new Pessoa("Carlos", 25);
        Pedra p = new Pedra();

        // Imprime "Meu nome é Carlos e tenho 25 anos".
        System.out.println(carlos.toString());

        // Imprime algo silimar a "Pedra@5c0d3c".
        System.out.println(p.toString());
    }
}

In this case above, the class Pessoa writes about the method toString(), so that when it is invoked, a String useful is produced. Already the class Pedra, does not overwrite this method, keeping with what was inherited from the class Object which is not a particularly useful method.

Browser other questions tagged

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