I was playing with the following question: Using Java 8's Optional with Stream::flatMap and wanted to add a method to a custom Optional<T>
and then check if it worked.
More precise, I wanted to add a stream()
to my CustomOptional<T>
that returns an empty stream if no value is present, or a stream with a single element if it is present.
However, I came to the conclusion that Optional<T>
is declared as final.
Why is this so? There are loads of classes that are not declared as final, and I personally do not see a reason here to declare Optional<T>
final.
As a second question, why can not all methods be final, if the worry is that they would be overridden, and leave the class non-final?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…