« October 2009
SunMonTueWedThuFriSat
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
22
23
24
25
26
27
28
29
30
31
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 768

Powered by Roller Weblogger.
« "helloworld" with... | Main
20091021 Wednesday October 21, 2009

Look Ma, javac tells me I am overriding static method wrongly!!


// File: SuperClass.java

public class SuperClass {
    public static int func() {
        return 0;
    }
}


// File: SubClass.java

public class SubClass extends SuperClass {
    public static boolean func() {
        return false;
    }
}

$ javac -fullversion
javac full version "1.6.0_15-b03-226"

javac SuperClass.java  SubClass.java 
SubClass.java:2: func() in SubClass cannot override func() in SuperClass; attempting to use incompatible return type
found   : boolean
required: int
    public static boolean func() { 
                          ^
1 error

The subclass uses a different return type for the same named method with same argument types. So, it is overloading SuperClass.func() and the overloading SubClass.func() differs only in return type. But, I am not sure of the error message....



( Oct 21 2009, 09:38:10 AM IST ) Permalink Comments [6] del.icio.us | furl | simpy | slashdot | technorati | digg

Trackback URL: http://blogs.sun.com/sundararajan/entry/look_ma_javac_tells_me
Comments:

Hey dude,

Firstly return type never helps in overloading.
Secondly here you are overriding method.
Third static methods can never be overridden.

Thanks
Vinod

Posted by Vinod Kumar Kashyap on October 21, 2009 at 11:20 AM IST #

Hi Vinod,

Thanks for commenting! Of course, I know that return type is not considered for overloading and I do know static methods can not be overriden. This blog entry is about appropriateness (or lack of it) error message from java compiler. BTW, I didn't deliberately write such a code -- JavaFX compiler uses javac in the back-end. It ended up generating such (wrong) code for a specific case and resulting error message looked odd (This is the next version of JavaFX compiler that is being developed with more or less rewrite)

Posted by A. Sundararajan on October 21, 2009 at 11:38 AM IST #

Actually you can override a static method, it's just that normally you call it with an explicit class. You can however call it with an implicit class, as per usual non-static methods.

SuperClass obj = new SubClass();
obj.func(); //What's the return value?

Regardless, it's pretty bad that JavaFX generated erroneous code.

Posted by Ryan on October 21, 2009 at 06:52 PM IST #

Ryan: Just a clarification: That bug I mentioned with JavaFX compiler is *not* in the released product versions of the JavaFX compiler. Compiler code was at that temporary wrong state during the development of new compiler - which is still being development. And it has been fixed since then. So, no real harm done to any JavaFX code out there.

Posted by A. Sundararajan on October 21, 2009 at 07:26 PM IST #

I agree that the javac error message is confusing. Perhaps it should say "func() in SubClass cannot hide func() in SuperClass", using the terminology of JLS 8.4.8.3.

Posted by Eamonn McManus on October 21, 2009 at 10:17 PM IST #

I presume you have filed a bug against javac...

Posted by Jonathan Gibbons on October 28, 2009 at 03:51 AM IST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed
Copyright (C) 2005, A. Sundararajan's Weblog