# 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]
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview