3

Why Collectors class is final in Java 8? I want to extend Collectors class in my own class MyCollectors. There I will add additional methods which is not existing available in Collectors class.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Prasath
  • 1,233
  • 2
  • 16
  • 38
  • Hey my question is not use of final class in java. Please go through my question. I want to extend Collectors class into MyCollectors class. – Prasath Feb 25 '19 at 12:29
  • https://stackoverflow.com/questions/5181578/what-is-the-point-of-final-class-in-java does not solves my problem – Prasath Feb 25 '19 at 12:30
  • Prasath, I have read your question. The simple, quick answer is: **You simply can't extend `Collectors`**. The longer answer, is that your question has little-to-no value for the community, since the **explanation** of *"why can't final classes have any children ?"* (classes that inherit from Collectors in this case) is explained in the aforementioned [question](https://stackoverflow.com/a/5181618/8075923). Eran's solution below should be satisfactory for you to understand why your initial logic was flawed. – Soutzikevich Feb 25 '19 at 12:36
  • 1
    There’s nothing stopping you from creating a class named `MyCollectors` and adding `Collector` factory method to it. What benefit should subclassing `Collectors` have? – Holger Feb 25 '19 at 14:31

2 Answers2

6

Collectors is final because it only contains static utility methods. It cannot be instantiated (its constructor is private), and therefore there would be no meaning to sub-classing it.

You can create your own MyCollectors class independent of Collectors. Or if you want to access Collectors methods via your MyCollectors, you can create wrappers of all the Collectors static methods within your MyCollectors class.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Yes I want to access Collectors methods via MyCollectors methods. Wrappers means do you want me to copy all the methods from Collectors class to MyCollectors class ? – Prasath Feb 25 '19 at 11:54
  • 1
    @Prasath It means copy the method signatures and have the MyCollectors methods call the corresponding Collectors methods. I wouldn't do that, though, since it seems pointless to me. – Eran Feb 25 '19 at 11:56
  • What happens if they added another new method in collectors class in next update? Do I need to do the same in MyCollectors class? If yes, then is any other solutions to my problem? – Prasath Feb 25 '19 at 12:01
  • 3
    @Prasath As I said, I wouldn't do that wrapping. Just use the two classes (`Collectors` and `MyCollectors`) together. You don't need a single class to contain all the collectors utility methods. – Eran Feb 25 '19 at 12:03
  • Ok I will keep my methods alone in MyCollectors class. – Prasath Feb 25 '19 at 12:30
1

If you need to implement your own collector, it is not about subclassing Collectors utility class, but about creating classes implementing Collector interface. As answered by Eran above

You can create your own MyCollectors class independent of Collectors. Or if you want to access Collectors methods via your MyCollectors, you can create wrappers of all the Collectors static methods within your MyCollectors class.

you can create your own class with methods returning your own implementation of Collector

Jan Hruby
  • 1,477
  • 1
  • 13
  • 26