Share via


final Modifier

Declares that a class cannot be extended or that a method or property cannot be overridden.

final statement

Arguments

  • statement
    Required. A class, method, or property definition.

Remarks

The final modifier is used to specify that a class cannot be extended or that a method or property cannot be overridden. This prevents other classes from changing the behavior of the class by overriding important functions. Methods with the final modifier can be hidden or overloaded by methods in derived classes.

Methods and properties in classes and classes can be marked with the final modifier. Interfaces, fields, and members of interfaces cannot take the final modifier.

You may not combine the final modifier with the other inheritance modifier (abstract). By default, class members are neither abstract nor final. The inheritance modifiers cannot be combined with the static modifier.

Example

The following example illustrates a use of the final modifier. The final modifier prevents the base-class method from being overridden by methods from the derived class.

class CBase {
   final function methodA() { print("Final methodA of CBase.") };
   function methodB() { print("Non-final methodB of CBase.") };
}

class CDerived extends CBase {
   function methodA() { print("methodA of CDerived.") };
   function methodB() { print("methodB of CDerived.") };
}

var baseInstance : CBase = new CDerived;
baseInstance.methodA();
baseInstance.methodB();

The output of this program show that the final method is not overridden:

Final methodA of CBase.
methodB of CDerived.

Requirements

Version .NET

See Also

Concepts

Scope of Variables and Constants

Type Annotation

Reference

abstract Modifier

hide Modifier

override Modifier

var Statement

function Statement

class Statement

Other Resources

Modifiers