8
When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract class or an abstract class? What is the best practice to join?
8
When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract class or an abstract class? What is the best practice to join?
11
There is no better option, you extend the class you need to extend. Whether it is abstract or not, it makes no difference to its code other than the fact that an abstract will possibly have methods not implemented and its new class will have an obligation to create an implementation for all abstract methods contained in the class (non-extabstracts are optional).
Whether you want to know if it’s better to create an abstract class or not depends on what you want. An abstract class cannot be instantiated, it is created to be used as a model for other classes. Non-eabstracts can be used as models but can also be instantiated directly. You only make an abstract class if you want to prohibit its instantiation (which is required if it is incomplete).
Although I think heritage should only be made of abstract class (or interface or trait if the language allows). Of course there are exceptions to the rule, but it seems more appropriate, but take this as my opinion, and I see it widening.
Of course, if the class has methods without implementation, functioning as contracts for derived classes to follow, that is, they act as an interface, then the class must be abstract. Unable to instantiate classes with methods without implementation.
For example, if you have a class Pessoa
and those derived from it PessoaFisica
and PessoaJuridica
. You probably don’t need and maybe can’t even instantiate just the Pessoa
. It is probably incomplete. You only created it to support the two (who knows others) derivatives I mentioned. It is almost an interface, but probably has variables and some methods with implementation. So Pessoa
probably should be abstract.
Remembering that can only inherit from one class. Abstract or not. Interface can several. At least in most languages.
When it comes to maintenance and readability of code, interfaces are a better indication than abstract classes, or this is independent of this choice?
It depends, of course. In general interfaces are better, but there are cases that can not use them under penalty of losing a basic structure of a kind. It is not possible to work only with interfaces and keep the reuse of code. In the abstract class background it should be compared with concrete classes. It is the concrete class that needs to omit some implementation details.
"You can only inherit a class" unless the language allows multiple inheritance. : ) I would also add "think three times before creating a hierarchy, be sure that the subclass and the superclass respect the IS-A ratio, and when creating methods in the superclass for code reuse think carefully if they really do apply to all subclasses" (I miss these points a lot).
@Piovezan is true, is that most of his questions have the tag [tag:java], so I went on this wave. I’ve given other answers, even to himself where I talk about the hierarchy, but worth the record here. Have too much inheritance abuse, some cases that are huge nonsense. Err in cases that seems to be a is-a, It’s one thing, getting it wrong is another :) People need to stop thinking of inheritance as default and yes as the ultimate solution.
Browser other questions tagged oop classes abstract-classes
You are not signed in. Login or sign up in order to post.
This guaranteed my [tag: orientation-to-objects] :)
– Maniero
Ai sim hein @bigown! My congratulations! One day I arrive at this level, rs
– Duds