Make Java parent class not part of the interface
Posted
by Bart van Heukelom
on Stack Overflow
See other posts from Stack Overflow
or by Bart van Heukelom
Published on 2010-06-12T11:50:56Z
Indexed on
2010/06/12
12:02 UTC
Read the original article
Hit count: 228
(This is a hypothetical question for discussion, I have no actual problem).
Say that I'm making an implementation of SortedSet
by extending LinkedHashMap
:
class LinkedHashSortedMapThing extends LinkedHashMap implements SortedSet {
...
}
Now programmers who use this class may do
LinkedHashMap x = new LinkedHashSortedMapThing();
But what if I consider the extending of LinkedHashMap
an implementation detail, and do not want it to be a part of the class' contract? If people use the line above, I can no longer freely change this detail without worrying about breaking existing code.
Is there any way to prevent this sort of thing, other than favouring composition over inheritance (which is not always possible due to private/protected members)?
© Stack Overflow or respective owner