How is the signature check of the super function

Asked

Viewed 112 times

1

The function super() serves to call the parent builder and verify suitable signatures.

public class Pessoa {
    private String nome;
    private String endereco;
    private String telefone;
    private String cpf;
    private String telefoneCelular;

    public Pessoa(String nome, String endereco, String telefone) {
        this.nome = nome;
        this.endereco = endereco;
        this.telefone = telefone;
    }
}

public class Aluno extends Pessoa {

    public Aluno() {
        super();
    }

    public Aluno(String curso, double[] notas, String nome, String endereco, String telefone) {
        super(nome, endereco, telefone);
        this.curso = curso;
        this.notas = notas;
    }
}

Although I know it works this way, I don’t understand how signature verification is done when the scope variables are different.

public class Aluno extends Pessoa {

    public Aluno() {
        super();
    }

    public Aluno(String curso, double[] notas, String teste1, String teste2, String teste3) {
        super(teste1, teste2, teste3);
        this.curso = curso;
        this.notas = notas;
    }
}

The check would be in the order in which the variables are declared within the super function? , because they are all of the same type (String).

  1. teste1 = name
  2. teste2 = address
  3. teste3 = phone
  • 2

    Exactly, if you pass teste2 as name, teste1 as phone and teste3 as address, the assignments will be made "wrong" because your implementation was made like this. Java does not check for the name you gave to the attribute to its type, if the type hits, it will assign the value, if not, it does not even compile.

  • Then the order I declare them within Student(...) will not matter, only will the statement be of importance within the super function()?.

  • 2

    If you change the order of the student constructor arguments in no way affects those of the super, since in the implementation within the student constructor, you correctly pass the arguments to the super().

  • 1

    Thank you very much for the clarifications.

1 answer

4


Method signatures only consider the number and types of parameters in their specific order, besides, of course the name of the method, their names do not matter. So if you have two methods with the same name and both have three parameters of the type String, error, because the signature is the same, even if the parameter names are different.

You are responsible for using the method correctly, as already stated in the comments.

If it is very important not to have confusion have a technique that I do not think is good in most situations. If you use only for this do not recommend.

Create classes for each of these members, then you’ll have 3 different types. Something like this:

public class Nome {
    private String nome;
    //e todo os resto aqui, construtor, métodos acessadores, operações, validações, etc.
}
public class Endereco {
    private String endereco;
    //e todo os resto aqui, construtor, métodos acessadores, operações, validações, etc.
}
public class Telefone {
    private String telefone;
    //e todo os resto aqui, construtor, métodos acessadores, operações, validações, etc.
}

I put in the Github for future reference.

Browser other questions tagged

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