___ ___ erkimt I extend an abstract class or concrete? ______ qstntxt ___

When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract or non-abstract class? What is the best practice to join?

    
______ azszpr89237 ___

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 unimplemented methods and its new class will have the obligation to create an implementation for all abstract methods contained in the class. optional).

If you want to know if it's better to create an abstract class or not, then it depends on what you want. An abstract class can not be instantiated. It is designed to be used as a template for other classes. Non-abstract can be used as models but can also be instantiated directly. You just make it an abstract class if you want to ban its instantiation (which is bound, if it is incomplete).

Of course, if the class has methods without implementation, they act as contracts for the derived classes to follow, that is, they function as if it were an interface, then the class must necessarily be abstract. Unable to instantiate classes with methods without implementation.

For example. If you have a class %code% and the derivatives of it %code% and %code% . Probably you do not need and maybe can not instantiate only the %code% . It is probably incomplete. You just created it to support the two (who knows other) derivatives I mentioned. It is almost an interface, but probably has variables and some methods with implementation. So %code% should probably be abstract.

Remembering that you can only inherit from a class. Abstract or not. Interface can several.

    
___

7

When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract or non-abstract class? What is the best practice to join?

    
asked by anonymous 27.09.2015 / 00:44

1 answer

10

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 unimplemented methods and its new class will have the obligation to create an implementation for all abstract methods contained in the class. optional).

If you want to know if it's better to create an abstract class or not, then it depends on what you want. An abstract class can not be instantiated. It is designed to be used as a template for other classes. Non-abstract can be used as models but can also be instantiated directly. You just make it an abstract class if you want to ban its instantiation (which is bound, if it is incomplete).

Of course, if the class has methods without implementation, they act as contracts for the derived classes to follow, that is, they function as if it were an interface, then the class must necessarily be abstract. Unable to instantiate classes with methods without implementation.

For example. If you have a class Pessoa and the derivatives of it PessoaFisica and PessoaJuridica . Probably you do not need and maybe can not instantiate only the Pessoa . It is probably incomplete. You just created it to support the two (who knows other) derivatives I mentioned. It is almost an interface, but probably has variables and some methods with implementation. So Pessoa should probably be abstract.

Remembering that you can only inherit from a class. Abstract or not. Interface can several.

    
27.09.2015 / 00:48