In the book The Philosophy of Java, a phrase like:

Recall the advice given at the end of the previous chapter: give preference to classes over interfaces. If the system architecture requires an interface, you will understand. In other cases, do not use interfaces unless absolutely necessary.

At the same time, almost in all the book on OOP and patterns it is said that preference should always (!) Be given to abstraction, and not to implementation.

I don’t have any real programming experience in Java, so such contradictory statements are confusing. Who to believe?

  • five
    depends on the tasks, for example, if you need one base, one i \ o mechanism, then, really, why interfaces, better abstraction right away. However, if you have 6 types of databases, then of course you should use interfaces. Ps this is just an example, each task should be considered separately - Senior Pomidor
  • @SeniorAutomator yes, but many books say that sleighs should be prepared in advance. That is, you always have to be ready for changes and extensions, even in small projects ... - faoxis
  • the right designed controller will allow working in several databases, for example, tibero, mysql ... - Senior Pomidor

1 answer 1

Further in the book there are such words:

"In general, it is recommended to give preference to classes over interfaces. Start with classes, and if the need for interfaces becomes obvious, redesign your project's architecture. Interfaces are a great tool, but they are often abused."

However, if the project has grown to a huge size, the phrase "rework architecture" looks very dubious. Somehow I can hardly imagine a big project in which it is so easy to say "redesign the architecture." This is the kind of error that is difficult to correct. In my opinion, even if there is only one implementation, it is better to use the interface. In addition, interfaces are usually accompanied by the removal of class dependencies from each other to an external resource (for example, in an xml file) - which is a necessity in large projects.