13.4.16 final Methods

Changing an instance method that is not final to be final may break compatibility with existing binaries that depend on the ability to override the method. If the test program:

class Super { void out() { System.out.println("out"); } }
class Test extends Super {


	public static void main(String[] args) {
		Test t = new Test();
		t.out();
	}
	void out() { super.out(); }
}

is compiled and executed, it produces the output:

out

Suppose that a new version of class Super is produced:

class Super { final void out() { System.out.println("!"); } }

If Super is recompiled but not Test, then running the new binary with the existing binary of Test results in a VerifyError because the class Test improperly tries to override the instance method out.

Changing a class (static) method that is not final to be final does not break compatibility with existing binaries, because the class of the actual method to be invoked is resolved at compile time.

Removing the final modifier from a method does not break compatibility with pre-existing binaries.