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?
The optional final payment is also sometimes known as the guaranteed minimum future value (GFMV). This term is used because, even if the value of your car were to drop more sharply than expected and the car was worth less than the optional final payment at the end of the contract, you wouldn't have to pay extra.
Optional is a container object which may or may not contain a non-null value. You must import java. util package to use this class. If a value is present, isPresent() will return true and get() will return the value.
Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava.
The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class. If you try it gives you a compile time error.
According to this page of the Java SE 8 API docs, Optional<T>
is a value based class. According to this page of the API docs, value-based classes have to be immutable.
Declaring all the methods in Optional<T>
as final will prevent the methods from being overridden, but that will not prevent an extending class from adding fields and methods. Extending the class and adding a field together with a method that changes the value of that field would make that subclass mutable and hence would allow the creation of a mutable Optional<T>
. The following is an example of such a subclass that could be created if Optional<T>
would not be declared final.
//Example created by @assylias
public class Sub<T> extends Optional<T> {
private T t;
public void set(T t) {
this.t = t;
}
}
Declaring Optional<T>
final prevents the creation of subclasses like the one above and hence guarantees Optional<T>
to be always immutable.
As others have stated Optional is a value based class and since it is a value based class it should be immutable which needs it to be final.
But we missed the point for this. One of the main reason why value based classes are immutable is to guarantee thread safety. Making it immutable makes it thread safe. Take for eg String or primitive wrappers like Integer or Float. They are declared final for similar reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With