R
R
rumasterov2016-09-14 11:17:58
OOP
rumasterov, 2016-09-14 11:17:58

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

3 answer(s)
S
Stanislav Makarov, 2016-09-14
@Nipheris

In my opinion, dependencies within the system from interfaces are more flexible.

Of course, dependencies on interfaces are more flexible, because do not force you to use any implementation (even a partial one). By using an interface, you are declaring the bare minimum of information required for the components to interact, and nothing more.
Each OO language has its own conventions for naming interfaces, but it's probably better to just call it Logger. You don't write Class at the end of every class name.
Interface - when you want to describe an interaction contract, in other words, to group several methods. The whole point of the interface is that it can only be fully implemented, not partially.
Abstract class - when you want to offer some partial implementation. It can be either an implementation of the previously described interface (which is more than normal), or an abstract class with its own public (including abstract) methods. In the second case, you simultaneously describe a certain interface and right there - its partial implementation, which can be used by the derived class.

M
Maxim Fedorov, 2016-09-14
@qonand

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.

D
Daniil Danilov, 2016-09-14
@kempendyi

Recommended reading:
sergeyteplyakov.blogspot.de/2014/12/what-are-inter...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question