2

I wanted to understand the purpose of creating such a class structure where once we extend some abstract implementation, while enforcing to implement a common interface again.
For an instance, in java.util generally the concrete classes implement an interface and also extend an abstract-class, while this abstract-class has already implemented the same interface.
ie.

HashMap class implements the Map interface and also extends AbstractMap abstract-class.
While, even the AbstractMap abstract-class implements the Map interface.

ClassDiagram

Satyendra
  • 1,635
  • 3
  • 19
  • 33
  • duplicate of : https://stackoverflow.com/questions/11175058/why-does-hashmap-implement-map-if-it-extends-abstractmap – akshaya pandey Feb 15 '18 at 12:56
  • This is semantically the same. It was maybe done for clarity (if you just look at `HashMap`). – Henry Feb 15 '18 at 12:56
  • 2
    Nice picture! It's too good to be wasted on a duplicate, so I re-used it in an edit of the question marked as an original for closing this one, if you don't mind. If you do mind, please let me know, and I'll undo the edit. – Sergey Kalinichenko Feb 15 '18 at 13:02
  • 1
    I agree that it's possible that the intention was clarity, but in my opinion it "achieves" the opposite result, at least that's how my brain is wired. Since extending a class that implements an interface necessarily results in a subclass that implements that interface like it or not, seeing an explicit `implements ThatInterface` in the subclass makes me wonder why, since I was operating under the assumption that my superclass already implemented that interface. So I have to go to check the superclass. In my opinion this practice is a good example of too much information. – SantiBailors Feb 15 '18 at 13:06
  • @SantiBailors Implementing an abstract class in `HashMap` is not essential to `HashMap` being a `Map`. That is why it is important to extends the interface directly. `AbstractMap`, on the other hand, is not *required* to extend the interface; it does so pretty much for reasons of better code validation by the compiler. [I added an answer to the original, that goes into this argument a little deeper](https://stackoverflow.com/a/48808308/335858). – Sergey Kalinichenko Feb 15 '18 at 13:27
  • @dasblinkenlight Thanks for the pointer, I think I follow and it makes sense. But honestly for the moment I cannot agree and I feel that in order to achieve the positive outcome you mentioned a good comment would have been better than actual code that wouldn't make any difference whether it was there or not; the latter is the type of coding that confuses me the most. But I might change my mind once I looked better into this subject, so I will start by studying your answer in detail. – SantiBailors Feb 15 '18 at 13:55
  • @SantiBailors that `HashMap` extends `AbstractMap` can be seen as an implementation detail while that is implements `Map` is part of its contract. Java does not allow to express this distinction clearly. – Henry Feb 15 '18 at 19:03
  • @Henry Thanks, I think I get it. You guys made a good point. – SantiBailors Feb 16 '18 at 07:38

0 Answers0