// 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....
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 #