In this case below, the login attribute would not have to be set to private for the method?

1

I'm studying inheritance in Java in one of the exercises has the code below, I think, the login attribute would not have to be set to private for the method?

public class Gerente extends Funcionario {

private int senha;

public void setSenha(int senha) {
    this.senha = senha;
}

public boolean autentica(int senha) {
    if(this.senha == senha) {
        return true;
    } else {
        return false;
    }
}

//novo método, recebendo dois params
public boolean autentica(String login, int senha) {
    //implementacao omitida
}

}

    
asked by anonymous 19.05.2018 / 17:04

1 answer

1

Without a context about the problem, it is difficult to say precisely.

But just looking at the code, I understand that at the time of authentication you will perform a comparison between the Manager values and values that came from the parameters.

Therefore, login :

//novo método, recebendo dois params
public boolean autentica(String login, int senha) {
    //implementacao omitida
}

Just like the password, already in Gerente :

private int senha;

It should instead be a private field of Gerente . So:

private String login;
private int senha;

This is because login and senha become to Gerente .

Then we would have something like this, using the above code:

Gerente gerente = new Gerente();
gerente.setSenha("senha");
gerente.setLogin("login");

boolen loginValido = gerente.autenticar("outroLogin", "outraSenha");

Or, better yet, without sets and using constructor:

Gerente gerente = new Gerente("login", "senha");
boolen loginValido = gerente.autenticar("outroLogin", "outraSenha");

If you want to evolve this model, usually login and senha are associated with Usuario . In this way, it would be interesting that Gerente had a Usuario with login and password.

    
19.05.2018 / 20:23