Chan Ag wrote:1. State of immutable object can not be modified after construction, any modification should result in new immutable object.
2. All fields of Immutable class should be final.
3. Object must be properly constructed i.e. object reference must not leak during construction process.
4. Object should be final in order to restrict sub-class for altering immutability of parent class.
I have understood points 1 and 2. However I could not understand points 3 and 4. How can object reference leak during construction process?
Maneesh Godbole wrote:I would also recommend going through http://www.javaranch.com/journal/2003/04/immutable.htm
Chan Ag wrote:Thanks so much, Ivan. I get it now.
Thank you..
Chan.
OCPJP 1.6
Vivek Raj wrote:
Chan Ag wrote:Thanks so much, Ivan. I get it now.
I still did not get how subclass the naughtyEmployee has affected the immutability of its superclass?
Henry Wong wrote:
Vivek Raj wrote:
Chan Ag wrote:Thanks so much, Ivan. I get it now.
I still did not get how subclass the naughtyEmployee has affected the immutability of its superclass?
I think you forgot that immutable objects will be used. It is not just immutability for immutability sake. Code depends on them to operate correctly. And that includes security -- imagine a parameter changing to a high secure resource *after* the instance passed the password check.
Yes, instances of the superclass is technically still immutable. However, instances of the subclass passes the IS-A test for the superclass. So, instances of the subclass are instances that IS-A superclass, can be used like the superclass, but is mutable. This will break code that depends on the immutability.
Henry
OCPJP 1.6
Consider Paul's rocket mass heater. |