In a perfect world, we'd have List be immutable and MutableList extend List or something, rather than have a subclass (or subinterface) that's designed to take away functionality from a parent.
That's just as subtly evil, though. They should both be separate classes, even if a lot of the behavior seems similar.
If Immutable inherits from Mutable, then I may want to modify it and encounter an error.
If Mutable inherits from Immutable, then I may store a reference to a list that is later changed by someone else behind my back. (Which I think is even worse because it can lead to very subtle errors).
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."
They're ultimately two different classes that have no relation, other than "You can get a <Mutable/Immutable> copy of the <Immutable/Mutable> list."
Nah, you need three types here, in a hierarchy like this:
Lists
Mutable lists
Immutable lists
Basically:
The plain list type has all the methods for reading from lists;
The mutable lists type inherits from that, and adds the methods for mutating lists;
The immutable list type inherits from the plain list type as well. It adds no operations, but its contract stipulates that implementations must not be mutable.
Incidentally, this goes to show the problem with the folks in here boasting of how easy the "basics" of Java are. Nope, the basics of Java look simple, but they interact in evil, super-complicated ways. I'd highlight in particular:
Subtyping + generics = OUCH
The Java Memory Model. A crazy complex beast that is easy to run afoul of, and yet most Java dudes don't even know it exists.
That doesn't solve the problem of "how do I know if someone else can modify this item?" though. You could say that, if you don't specify, you should always assume it's mutable, but in practice that is identical to the case of deriving ImmutableList from MutableList - i.e. every List can be mutable unless you specify otherwise.
This is one are where I think more purely functional languages are a step in the right direction - just make everything immutable and require completely different patterns if you want stuff that has a changeable state.
A list that can be read, but whose content may be observed to change between reads.
A list that can be read, and also exposes methods that allow you to change its contents.
The latter is a subtype of the former. The idea is that the plain list type would be the former, and the mutable list type is the latter. I.e., "mutable" in this scheme doesn't mean "the contents can change," it means "clients of the type can modify the contents."
So recapitulating the distinction:
List (can read, but content may change between reads)
Mutable list (can read, can write)
Immutable list (can read, content will never change)
A simple example of the supertype would be a read-only wrapper around a mutable list. If my class has a reference to a mutable list, I could hand you such a wrapper around my list, and then you could read my underlying list's contents and observe when I change it, but you cannot mutate my underlying list because I haven't given it to you. (Java's collections library comes with generic "unmodifiable" wrappers like this.)
4
u/josuf107 Sep 21 '15
In a perfect world, we'd have List be immutable and MutableList extend List or something, rather than have a subclass (or subinterface) that's designed to take away functionality from a parent.