Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Optional<T> declared as a final class?

Tags:

java

java-8

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?

like image 586
skiwi Avatar asked Mar 29 '14 17:03

skiwi


People also ask

Why is Optional 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.

What is Optional T in Java?

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.

What is the purpose of Optional class in Java?

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.

What happens if I declare employee class as final class?

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.


2 Answers

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.

like image 193
SharpKnight Avatar answered Oct 27 '22 17:10

SharpKnight


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.

like image 41
Aniket Thakur Avatar answered Oct 27 '22 16:10

Aniket Thakur