In this case below, wouldn’t the login attribute have to be set as private for the method?

Asked

Viewed 38 times

1

I am studying inheritance in Java and one of the exercises has the code below, I think, the login attribute would not have to be set as 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
}

}

  • There is no "attribute" login in this code.

1 answer

1


Without a context about the problem, it’s hard to say with precision.

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

Therefore, the login:

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

Like the password, already existing on Gerente:

private int senha;

It must be a private field of Gerente. Thus:

private String login;
private int senha;

This is because login and senha belong at the Gerente.

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

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

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

Or better yet, without the sets and using builder:

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 a Usuario. In this way, it would be interesting that Gerente had a Usuario with login and password.

  • Sorry for the lack of context, I forgot to put <br/> files. I pasted my files in PASTEBIN: <br/> Funcionario.java --> https://pastebin.com/5xrYHzBS <br/> Gerente.java ------> https://pastebin.com/ZJ8yMwuJ <br/> Testegerente.java -> https://pastebin.com/wmJ5vpUm <br/> Testefuncio --> https://Pastebin.com/Pruz1y7j <br/> <br/> And one more theoretical question, I appreciate the attention of td! <br/>

  • I saw the rest of the code and it seems to me that the solution I proposed meets. If possible, edit your question and put the code in your question.

  • @Downvoter, why voted negative?

  • It wasn’t me who said no, but unfortunately that "@Downvoter" mark doesn’t notify who voted for the question :/

  • @Articuno, yes, it was only a way of referencing who voted negative, rs

  • I also did not negative, but I can not positive, always falls to -1

  • @weltonvaz, quiet. See if my answer answers.

  • 1

    Your reply was excellent took doubts that my instructor was not able to help. Thanks!

Show 3 more comments

Browser other questions tagged

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