A method for several classes

Asked

Viewed 114 times

2

I was reading that the use of inheritance is almost "wrong" (I saw in a matter of Caellum), that the right is to use interface. So far, okay, I’ve made an interface, only the interface only lets me put the signature of the method, and I want everyone to use the interface to actually use a body method. Ex: my interface is person. I want all classes that implement person, use the method formatarCPF(), however, the interface does not let me do that, then I will have to make every class that implements person, have the code of formatarCpf() copied within the class.

I’m new, but the idea of the POO is not exactly copying code?

  • 4

    Inheritance is not wrong. People use it wrong. It is correct to use inheritance when inheritance should be used, just as it is correct to use the interface where it should be used. Do not believe blindly in everything you read, often (the vast majority) nor does the author of the text know what he is talking about. From what you described neither inheritance nor interface is the solution. "the idea of the POO is not exactly copy code?" No, this you do in procedural also with functions.

1 answer

4


There are some mistakes there. Inheritance is not almost wrong, it’s right when it makes sense, usually when attending the liskov principle. When the inheritance is not appropriate then the composition is more interesting.

It is common, but not mandatory that it should only inherit from abstract classes or interfaces, so inheritance is right, because inheriting from them is still inheritance in some way. The problem is you just have to think about whether you should inherit it from the concrete class, that’s all. In general when inheritance of concrete class is mixing concepts and tends to confuse objects, but there are cases that are exceptions, so it is allowed.

It is not true that interface only has signature, at least since Java 8, so long ago this is not true, so you inherit it normally, before you inherited the type but not the implementation.

You can only model object correctly knowing all the requirements (which are almost always not properly taken), but I can say that it seems that you need an abstract class Pessoa and not an interface. It seems that there should be a concrete class inherited from this class that could be PessoaFisica since it has a CPF. The CPF could not be in Pessoa because a person can be legal and not have CPF.

Even if I opted for the interface I could have any method I wanted. I just don’t know if formatting should be part of the class that models a person. A person should not format CPF, should have a class just to format things or until the CPF auto-format itself in its own class, following the SRP.

I am not saying to do, but an easier way to model is to forget OO, since it is difficult to do so, contrary to what people imagine.

Code reuse and not "do not copy code" is an object orientation feature, but not unique to it. Anyway, it looks like you read some material that’s giving you bad directions. So I always say that material indication is complicated. You may be reading material indicated by a lot of people and it still be bad or wrong. People learn wrong with a material and start pointing it as if it were good, even because she does not have the necessary critical sense. IS fake news everywhere.

You’re still a long way from understanding object orientation and you’re not modeling well, perhaps because you’re following a cake recipe passed through some material. Modeling is thinking, it is understanding the whole. It has some rules, but not to follow, it is only to guide. You have to understand why they exist to model right.

Browser other questions tagged

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