Override Object.toString Error

Why does this cause an error in Flash Builder ?:

package { public class Foo { override public function toString():String { return "Foo"; } } } 

The completion of the tabulation suggests that it is redefinable ...

Error message:

 Multiple markers at this line: -public -1020: Method marked override must override another method. -overridesObject.toString 
+7
tostring override flex flash-builder flex4
source share
2 answers

Remove override by toString() method.

There is a popular misconception regarding the toString() method, namely: if you want to provide a custom implementation of the superclass method, the override keyword is necessary. But in the case of Object , toString() is dynamic and attaches at runtime, denying the need to override. Instead, an implementation should be provided by the developer, so it is not created at run time. You just need to write your own implementation of toString():String .

+9
source share

Foo does not extend the class; therefore there are no methods to override. Just remove the override keyword from the function definition and it should compile

 package { public class Foo { public function toString():String { return "Foo"; } } } 

I will add that toString () is an Object method from which many ActionScript classes extend. But even if you extend Object, you do not need to override the toString () method. From the docs:

To override this method in a subclass of Object, do not use the override keyword.

Like this:

 package { public class Foo extends Object { public function toString():String { return "Foo"; } } } 
0
source share

All Articles