Is there a scenario where the Abstract class is preferred over the interface

I have a scenario in which there are several subclasses that have similar implementations and some additional methods for which implementations differ for each subclass. I suppose an abstract class would be a good choice for this scenario. But it would be better if this abstract class implemented an interface containing all the method declarations. Or should I just stick with an abstract class.

In short, I would like to know scenarios where I should prefer abstract classes at the top of the hierarchy rather than in the interface.

+6
source share
6 answers

Use an abstract class if you have subclasses - this is relative to the abstract class.

You can have both an abstract class and an interface - abstract classes that define implementations, and an interface that defines APIs.

Collection frames are an example of having an ArrayList extends AbstractList implements List

+4
source

An abstract class does not have to be completely abstract. You can define specific functions (and variables) that all as-is subclasses will use, and leave only certain methods that will be implemented by subclasses. The interface has the limitation that no functions can be defined.

Interfaces, on the other hand, allow flexibility for a class to implement multiple interfaces, while a class can distribute only one class. In this sense, an interface is likely to always be preferable to a purely abstract class. But for abstract classes, there are still many uses that contain some reusable functions.

+5
source

An abstract class can provide default behavior when interfaces cannot. This manifests itself when part of the behavior is spread across several subclasses.

It is very useful to use a method template template: http://en.wikipedia.org/wiki/Template_method_pattern , which reduce serial communication.

+2
source

Remember that with the Abstract class you can define data that has subclasses. With an interface, you can only define methods that developers must implement. So, in your situation, do you need common data and common methods or simple methods?

+1
source

Abstract classes allow you to transfer some code / data, which can then be used in inherited classes. They are great for this, but use inheritance very sparingly. Only inherit from the class if the new class is completely interchangeable with the abstract.

Interfaces do not contain code.

I prefer to code interfaces when possible. I also want these interfaces to be as small as possible. This leaves me with the opportunity to change the initial impimentation at a later time.

If you are coding an abstract class, it is more difficult to replace it with a later time.

You can apply an interface (or several small interfaces) to an abstract class. Seems like this might be your best approach.

0
source

Always use the interface. You should try very hard to avoid abstract classes. With that said, abstract classes take place, especially in library code, similar to java collections. In these places, you create classes specifically designed for expansion, and there is great value for consolidating behavior, especially in terms of quality.

0
source

Source: https://habr.com/ru/post/891208/


All Articles