Variables and methods in the abstract class, where to put?

Asked

Viewed 134 times

3

Cliente and Fornecedor has name, phone and email in common what is the best way for me to treat this, should I create these variables in common in the abstract class? or is there a better way for me to do that?

Pessoa (Abstract):

public abstract class Pessoa {

    public abstract String getNome();
    public abstract String getEmail();
    public abstract String getTelefone();  
}

Customers:

public class Cliente extends Pessoa{

    public String nome;
    public String email;
    public String telefone;
    public String cpf;
    public String rg;

Cliente(String nome, String email, String telefone,String cpf, String rg){
    this.nome=nome;
    this.email=email;
    this.telefone=telefone;
    this.cpf=cpf;
    this.rg=rg;
}

public String getNome(){ return this.nome;}
public String getEmail(){return email;}
public String getTelefone(){return this.telefone;}
public String getCpf(){return this.cpf;}
public String getRg(){return this.rg;}   
}

Supplier:

public class Fornecedor extends Pessoa{

    public String nome;
    public String email;
    public String telefone;
    public String cnpj;
    public Long inscricaoestadual;

    Fornecedor(String nome, String email, String telefone,String cnpj, Long 
    inscricaoestadual){
        this.nome=nome;
        this.email=email;
        this.telefone=telefone;
        this.cnpj=cnpj;
        this.inscricaoestadual=inscricaoestadual;
    }

    public String getNome(){ return this.nome;}
    public String getEmail(){return email;}
    public String getTelefone(){return this.telefone;}
    public String getCnpj(){return this.cnpj;}
    public Long getIncricaoestadual(){return this.inscricaoestadual;}

}

1 answer

7


It should create all that is common in the abstract class, both fields and methods, which is possible because it is the same in all classes. Even if the behavior of the methods are potentially different you can put and overwrite them if it is different in the inherited class, but the contract is already in the upper class.

Other problems

Even the methods don’t make much sense getters be abstract because they are likely to have standard and equal implementation for all classes. It might make sense that they’re not final, although you might question that.

Actually, you can question the use of getters and setters. It’s kind of become a pattern that everybody does this without even understanding why, people do it because they’ve seen it somewhere they’re supposed to. But there are also those who say you should not use them. I have already said this in several questions, one of which can serve as a reference to start studying the subject is Getters and Setters Methods. It’s best to use methods that tell you what you’re doing, you trade a mechanism for domain behavior. If it is to use mechanism then why not use the field directly? It has less problem than people imagine when you choose the mechanism because it is already abstraction leak.

There is another conceptual problem because doing this inheritance may have more than one object in the system for the same entity (the person). It seems quite wrong and violates the DRY data (the same time person is seen as a customer and time is seen with a supplier). I have talked about this in several questions:

And there are others to research right here. Unfortunately it adopted a wrong pattern decades ago and then as almost everyone just follows ready models continue to do wrong. And then they blame the "old" programming paradigm, the procedural, for the wrong "decisions" of their own, and they think that talking about object-oriented programming is all right. Cohesiveness is the name of the game, independent of the paradigm. This form is not cohesive.

If you go deep, you also violate the SRP by keeping in the same object data that belongs to an entity (person) and a role (customer or supplier). It also violates the LSP because it engages many different things. Prefer composition to inheritance. In a way violates the IAPA of SOLID, because he confuses himself a little with the SRP. Examples of OO often teach wrong because they create artificial hierarchies that make it look like this kind of inheritance is good. There’s a good and bad side to SOLID.

In some cases it may make sense to create a single entity that has all the roles, mainly as database optimization avoiding a ratio 1:1. The problem is that it doesn’t look very good in memory (you can use it), then you have the call impedance Ismatch model and why people use Orms adding complexity to the software. That’s why I prefer to model in memory like the database, it’s so much simpler...

long for state enrollment? This doesn’t make sense, I don’t know why you hit the others and missed this one, maybe because you’re doing random things and hitting by coincidence. CPF or CNPJ field type in VARCHAR or INT database?.

  • 2

    I understand your position. But I cannot see an answer in your answer, the way forward so that I can resolve my doubt. good everything I write I learn in college, of course you will criticize but I do not get restricted to classroom too, so I am here, you sent thousands of information I ended up getting lost and I do not know where begins or ends the "error" or bad practice.

  • 2

    That’s a problem nowadays. Everyone wants it ready and can’t build knowledge. Just read the first paragraph and forget the rest. Leave the rest to the people who want to learn to do it right. The first sentence is what you want. I’m sorry, I always try to teach right and forget that people just want the cake recipe.

  • Analyzing your answer more calmly I see that it is more complete than just an answer to my question, thank you very much, sorry for previous messages.

Browser other questions tagged

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