Why is this class mutable?
An arbitrary instance of Test
isn't guaranteed to be immutable, although direct instances of Test
are. But consider this subclass:
public class MutableTest extends Test {
private int mutable;
public MutableTest(String url) {
super(url);
}
@Override
public String getUrl() {
return super.getUrl() + mutable++;
}
}
Then you can write something like this:
Test instance = new MutableTest("http://example.com/");
String firstGet = instance.getUrl();
String secondGet = instance.getUrl();
assertEquals(firstGet, secondGet); // Boom!