Today while I was reading some documentation regarding the BigDecimal class,
I’ve stumbled upon an fundamental property, the BigDecimal class is Immutable.
How could I explain to my grandmother the immutability concept ?
What are the pro and cons of the immutability of a class ?
Can an extended class become mutable ?
Considering that I want to extend the BigDecimal with my class:
`MyBigDecimal extends BigDecimal`
Does the extending violate the basic Object Oriented design principles ?
Check this question: What is meant by immutable
Or from Effective Java:
Pros:
Cons:
Yes – which is why immutable class should be made final (or alternatively, make all constructors private and provide factories to create new objects).
BigDecimal is a good example of what should not be done when creating an immutable class (it can be extended which can cause issues as you mentioned in your question).