Why immutable classes are final




















Prior to going ahead do go through characteristics of immutability in order to have a good understanding while implementing the same.

Data members in the class must be declared private so that direct access is not allowed. Deep Copy of objects should be performed in the getter methods to return a copy rather than returning the actual object reference Note: There should be no setters or in simpler terms, there should be no option to change the value of the instance variable.

Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. HashMap; import java. It has three final data members, a parameterized constructor, and getter methods. Please note that there is no setter method here. Latest Insider. Check out the latest Insider stories here. More from the IDG Network.

How to use Java generics to avoid ClassCastExceptions. Java Elementary language features. How to use immutability in C. Java 9's other new enhancements, Part 2. His comment was: "Implementation inheritance explicitly disallowed. Immutable classes must prohibit extension which succinctly describes a primary rationale for specifying an immutable Java class as final : By declaring immutable classes final, we impose a guarantee that malicious subclasses capable of changing the state of the object and violating assumptions that clients often make regarding immutability, are not a concern.

The authors suggest that making the immutable class final would be one way to deal with a situation in which "malicious code may try to subclass an immutable class" and "masquerade" as its parent type, In Mutable and Immutable Objects , David O'Meara specifies that there are two approaches for ensuring methods are not overridden in a class designed to be immutable.

If you don't mark the class final , it might be possible for me to suddenly make your seemingly immutable class actually mutable. For example, consider this code:. Notice that in my Mutable subclass, I've overridden the behavior of getValue to read a new, mutable field declared in my subclass.

As a result, your class, which initially looks immutable, really isn't immutable. I can pass this Mutable object wherever an Immutable object is expected, which could do Very Bad Things to code assuming the object is truly immutable.

Marking the base class final prevents this from happening. Contrary to what many people believe, making an immutable class final is not required. The standard argument for making immutable classes final is that if you don't do this, then subclasses can add mutability, thereby violating the contract of the superclass. Clients of the class will assume immutability, but will be surprised when something mutates out from under them. If you take this argument to its logical extreme, then all methods should be made final , as otherwise a subclass could override a method in a way that doesn't conform to the contract of its superclass.

Previous Page Print Page. Next Page. Dashboard Logout.



0コメント

  • 1000 / 1000