E
E
EarthFM2014-08-22 15:31:43
Java
EarthFM, 2014-08-22 15:31:43

Why is an interface needed in java?

Why do I need an interface in java, if I can directly declare methods with the same names in different classes. I understand that an interface is either an abstract class or a semantic construct.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
pi314, 2014-08-22
@EarthFM

To implement modularity and loose coupling . An interface is, in essence, a contract (for a "supplied" set and method signatures).
Its fundamental difference from an abstract class is that in Java there is no multiple inheritance and, accordingly, a concrete class can only inherit from one abstract one. But he can implement different interfaces as much as he wants .
If, however, we take into account that there are also abstract methods, it becomes clear that an abstract class/methods and an interface are tools for different tasks.
A classic example of the proper use of interfaces is Java collections. Estimate, given the considerations above, what a mess it would be to use if the API was made not through interfaces, but through abstract classes :)

S
Sergey, 2014-08-22
Protko @Fesor

A classic example is the logger class. You can simply not take a steam bath and make the Logger class with its own implementation, and everywhere in the client code (that is, the code that this class will use) tie to this class.
But chu, now our logs should be stored not in files but in the database, all of a sudden. We change the constructor, we change the implementation of all methods (suppose that we have one, log(string message, int level, string category);
And it’s good if we follow the principle of dependency inversion, then the fact that we changed the constructor will not affect the client code in any way, because the instance is simply given to him and he does not know anything about how to create it. But we have a problem, either by searching and replacing to change the type in the client code, or to be inherited and simply replace the implementation. In this regard, inheritance will work as we want, but why?
With an interface, we have a beautiful interface, we have its implementations, if we have a dependency container, then we can bind a specific implementation to the interface, and where this interface is required, the necessary implementation will be automatically inserted. Replacing the implementation - change the config DI.
Also, if you need to change the interface often, then you already have some problems with building the architecture. The interface should be simple and should do one thing.
In general, read about GRASP and SOLID.

O
OnYourLips, 2014-08-22
@OnYourLips

An interface is a contract by which your code is interacted with.

L
lookid, 2014-08-22
@lookid

https://en.wikipedia.org/wiki/GRASP
Identical names are out of the question here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question