-4

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

Why HashSet extends AbstractSet and implements Set, as AbstractSet already implements Set?

sudarshan
  • 87
  • 1
  • 4

2 Answers2

0

A HashSet is a Set. It implements the contract defined by the Set interface. Therefore it's useful to make it clear that it implements that interface, without having to check the class hierarchy.

The fact that it extends AbstractSet<E> is just an implementation detail. It might stop extending AbstractSet in a future Java version (though probably not very likely), but it would always implement Set.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Of course, it will work properly without implementing the set interface. But it is done for making the code more readable so that any programmer can understand it properly without seeing the full hierarchy of class. And also for minimizing the risk can be happen in future (if HashSet does not implement the AbstructSet, although it is not very likely).