Answer the question
In order to leave comments, you need to log in
Is it better to build dependencies within the system from interfaces or abstract classes? When is it better to use an abstract class and when an interface?
For example, we have a logging component. We can create an AbstractLogger with a default implementation and inherit all our loggers from it, specify in the constructors or setters of other classes that they accept a logger of the AbstractLogger type as a parameter.
To what extent is this correct? Maybe it's better to use an interface? For example, we can create a LoggerInterface, in the constructors or setters of other classes, indicate what they take in the parameter of the logger of the LoggerInterface type, and for example, implement the default implementation in the AbstractLogger which implements several methods from the LoggerInterface, so I can decide for myself whether to inherit from the AbstractLogger which for I have already implemented several methods from the interface, or if this implementation does not suit me, I can write my own by implementing LoggerInterface.
In my opinion, dependencies within the system from interfaces are more flexible.
Can you please tell me how to correctly determine when to use an interface and when an abstract class?
Answer the question
In order to leave comments, you need to log in
In my opinion, dependencies within the system from interfaces are more flexible.
An abstract class allows you to partially implement functionality and specify what should be implemented in descendants, this is convenient for both the base class. But it is not worth using it as an implementation of a dependency mechanism, because:
1. There may be situations when you need to implement a class without inheriting from an abstract class.
2. One class can be used in several dependencies, and each of them can have its own set of required characteristics. In this case, you will have to "inflate" the base class, which is not good.
Interfaces simply describe the structure of functionality. In fact, it doesn’t matter to you to know how one or another class implements the functionality, the main thing is to understand what it can do. From this point of view, the interface will fit perfectly.
Recommended reading:
sergeyteplyakov.blogspot.de/2014/12/what-are-inter...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question