Wednesday 19 December 2018

Immutability

How to break immutability if class
1. has private fields
2. no setter method
3. proper constructor for obj creation.
but failed to.mark the class as final.

then effectively, you created your immutable class and told everyone that, use my object, wherever for all immutable features, like storing immutable object as key

but since its nit final, any class
1. can extend it.
2. provide a setter functionality for its own private varible ,hence not immutable.
3. type cast this immutable object to mutable type reference.

and start passing where you had passed your immutable object , breaking your promises.

so.mark it final, so that no one can extend it.

No comments:

Post a Comment