I've got two classes, Base
and Sub
. I can freely make changes to Base
. But Sub
is written by a third party and I don't have privileges to make changes to it. Here's what they look like:
public class Base {
public void prepareForMission() {
loadUpInfantry();
loadUpWeapons();
}
}
public class Sub extends Base {
public void prepareForMission() {
super.prepareForMission();
loadUpOxygen();
}
}`</pre>Suppose that I need to change the behaviour for instances of `Base` without changing the behaviour of `Sub` or other subclasses.
The horrible, ugly hack I came up with for this is to evade polymorphism like so:
<pre class="prettyprint">`public class Base {
public void prepareForMission() {
loadUpInfantry();
loadUpWeapons();
_if (getClass() == Base.class) {_
loadUpSecretPlans();
}
}
}
Doesn't it make ya cringe? I'm quite ashamed of it.