# Monday, 07 November 2011
« Java Method Overriding Is FUBAR Part 5 o... | Main | New Development Snapshot »
Java Method Overriding Is FUBAR Part 6 of ∞

Parts 1 through 5 described behavior that is new in JDK 7, but there are also pre-existing bugs.

When you declare a static method as final (which makes no sense and there is no good reason this was ever allowed) you disallow a virtual method in a base class from being overridden:

public class A
{
  public void foo() {
    System.out.println("A.foo");
  }
}

public class B extends A
{
  public static final void foo() {
    System.out.println("B.foo");
  }
}

public class C extends B
{
  public void foo() {
    System.out.println("C.foo");
  }
}

public class test
{
  public static void main(String[] args) {
    new C();
  }
}

After you compile this (the easiest trick is probably to rename the B.foo method to f__ and after you compile use a hex editor to patch the resulting class file) and run it, you get:

Exception in thread "main" java.lang.VerifyError: class C overrides final method foo.()V
        at ...

Like I said, this is not new. I also tried this on JDK 1.1 and JDK 1.5 and on 1.1 behaves as expected and 1.5 has the modern behavior.

Monday, 07 November 2011 07:12:25 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]