Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6322344

Compiler throws misleading error report when interface method implemented in anonymous inner class.

XMLWordPrintable

    • generic
    • generic

      Tried in windows. JDK 1.6.0-ea-b32

      Compiler throws misleading error report when interface method implemented in anonymous inner class.

      Please see the following code:
      <code>
      interface Foo{
              public void bar();
      }

      public class TestInterface {
          Foo foo = new Foo() {
              public void notBar(){
                  System.out.println("Bar...");
              }
          };
      }
      </code>

      Output of the code when compiled:
      <output>
      E:\javatrails\compilerAPI>javac -Xlint TestInterface.java
      TestInterface.java:7: <anonymous TestInterface$1> is not abstract and does not o
      verride abstract method bar() in Foo
              public void notBar(){
                          ^
      1 error
      </output>
      The error pointer is wrong and misleading.
      I think the following will be more meaningful.
      Foo foo = new Foo() {
      ^

      If I remove the methods decalration then compiler will not throw any line number

      <code>
      interface Foo{
              public void bar();
      }

      public class TestInterface {
          Foo foo = new Foo() {
          };
      }
      </code>

      <output>
      E:\javatrails\compilerAPI>javac -Xlint TestInterface.java
      error: <anonymous TestInterface$1> is not abstract and does not override abstrac
      t method bar() in Foo
      1 error
      </output>

            Unassigned Unassigned
            savadhansunw Seetharama Avadhanam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: