Interfaces can be instantiated?

1

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

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

This second line that left me confused. nameField is an instance of EditText . The getText() method is called by the object. This method returns an object of type Editable , which calls the toString() method? Is my interpretation correct?

Why the Editable class is an interface. How can it be instantiated in this example? Can anyone clarify what's going on?

    
asked by anonymous 29.10.2017 / 06:50

1 answer

4

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

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

You can not instantiate interfaces directly, but you can intanciar 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 the variables are interfaces, but what is actually instantiated are classes that implement them.

Back to your code, we then have the first line variable type a EditText (a class). When calling getText() , an object of 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 toString() method is invoked, thus supplying a String .

Every object has a method called toString() because this method is defined in class Object and since all other classes are subclasses of Object , 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 Pessoa class writes the toString() method, so that when it is invoked, a String useful is produced. Since the Pedra class does not override this method, getting what was inherited from the Object class is not a particularly useful method.

    
29.10.2017 / 07:53