Why can't I write "implements AClass" in Java?

In Java, people often define an interface with a class and use the interface name rather than the class name, where possible, to allow new implementations later. This duplicates the logical interface.

This type of duplication "just in case" is not needed if Java allows you to use the class as an interface, for example: class MyScanner extends MyStuff implements java.util.Scanner . It will also ease the situation when I need to provide a class type, but I do not want to extend this class.

As I understand it, the "implementation of the class" will be rejected not only by the compiler, but also by the JVM (if I cracked this declaration into a class file). Are there any technical difficulties for this or are not considered important things? This doesn't seem like a backward compatibility issue (I mean, the old code will work fine if the JVM supports this).

EDIT: for clarification, I will copy here StriplingWarrior a much better wording of the same questions:

Why can't a class implement another class method without actually extending this class? Is this a technical question? Does this somehow reveal to us some problems that the OP cannot foresee?
+4
source share
3 answers

Do I understand correctly that MyClass implements AClass means that MyClass must provide (or inherit) implementations for all public methods that AClass has? That is, each class implicitly defines an interface consisting of its public methods?

If so, then the problem I see with this is that the interfaces are really very different from the classes, and the two should not mix. Interfaces define a contract, classes implement. The consequence of this is that the functionality of classes can expand relatively freely, but does not interact with existing implementations in this way.

So, you have a very specific reason why your suggestion would be bad: you can never add any public method to any class without risking breaking some code that implements this class.

And on the other hand, what problems will he solve? Code duplication when you have a single implementation interface? How about resolving this without doing this? IMO this is usually done by people doing dogmatic TDD using outdated mocking frameworks that aren't able to taunt specific classes.

+9
source

The problem of multiple inheritance . If you try to extend or implement two classes, there are potential semantic problems.

You can implement several interfaces, as well as extend one class. But you can implement interfaces that cover most of the problems with multiple inheritance, just forcing the specification to be declarative in nature, requiring the developer to implement these methods directly. In this case, method conflicts are resolved in a simple way of implementing one method and counting for both interfaces.

+5
source

As already mentioned, the standard is defined in this way and is not included in the decision-making process. I can only fool my $ 0.02.

These are not so much duplicate logical interfaces as the standard for creating an API that you would like to be flexible. This is considered good design practice, even in languages ​​that support multiple inheritance, to provide APIs.

Given the current state of the IDE, it’s pretty simple not to do this in advance, and then reorganize later when you decide that you need several implementations of this type.

There may also be cases of use against this, where inside your code you have a strong dependence on the functionality of the class and due to another implementation that your code may break. This is the meaning:

 ClassA a = new ClassA(); 

but not

 InterfaceA a = new ClassA(); 

This can lead to unexpected behavior.

+1
source

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


All Articles