+ 1

Is it possible to override a method of a collection?

collection, override...

28th Nov 2016, 5:46 PM
Lucas Tavares
2 odpowiedzi
+ 3
Have a look at HashSet, LinkedList or HashMap source code for example and you will find that methods are final, meaning you can't override it. But you still can implement Collection's interface if you want to provide your own implementation of a collection. This way you can encapsulate official collection classes and "override" their behavior. It should be seen as a kind of view (in sql sens of the word) of a collection as I would not recommend you to write your own substitute to collection classes, those are really optimized, unless you don't want to use big data sets.
28th Nov 2016, 7:57 PM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
0
Yes it may differ but their are some limitations. Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned.
28th Nov 2016, 7:53 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar