2007年6月11日星期一

Prevent Overriding and Subclassing with final

The final keyword (Toplevel final keyword) is an attribute for classes and methods that lets you prevent overriding (methods) and subclassing (classes).

When a method is marked as final, no subclasses of that class can create a method of the same name that overrides that method. This ensures that when the method is called, it will always be the method marked final.

ActionScript Code:

final public function methodName() {
// your statements here
}



Similarly, for classes, if a class is marked final, no subclasses can be created that extend that class.

ActionScript Code:

package{
final
public class ClassName {
// your statements here
}
}



Note: it is pointless to have methods marked as final in a class marked as final since that class cannot be extended. Since it cannot be extended, there would be no way for methods to be overridden as that only occurs within subclasses of the original class.

0 评论: