JScript modifiers change the behavior and visibility of classes, interfaces, or members of classes or interfaces. You may use modifiers when defining classes and interfaces, but they are usually not required.
Visibility Modifiers
Visibility modifiers restrict how outside code accesses classes, interfaces, and their members. You can use restrictions to encourage good object-oriented programming practices by preventing calls to specialized internal methods and fields.
By default, any code that can access a class can access any of the members of that class. Using the visibility modifiers, you can selectively prevent outside code from accessing particular class members, allow only classes from the same package to access members, or allow only derived classes to access class members.
Visibility modifiers cannot be applied to global functions or variables. The only visibility modifiers that can be used together are protected and internal.
Visibility Modifier | Valid for | Meaning |
---|---|---|
class, class member, interface, interface member, or enumerations |
Member is visible to any code that has access to the class with no restrictions on visibility. By default in JScript, classes, interfaces, and their members are public. |
|
class member |
Member is visible only within the class in which it is declared. It is not visible to derived classes. Code outside the current class cannot access private members. |
|
class member |
Member is visible only within the class in which it is declared and to any derived classes of that class. The protected modifier cannot be used for classes at package scope, but it can be used for nested classes. |
|
class, class member, enumeration |
Class, class member, or enumeration is visible everywhere within the package in which it is declared. It is not visible outside the package. |
Inheritance Modifiers
Inheritance modifiers control how methods and properties from derived classes override methods and properties in a base class. By using this control, you can manage whether methods from derived classes will override a class you create.
By default, methods from a derived class will override base class methods unless the version-safe hide attribute is used in the derived class. This attribute helps prevent overriding. Using inheritance modifiers enables you to help control whether particular methods are overridden always or never.
In some situations, you may need to ensure that a base class method is not overridden. For example, if you define a class in a package, you can use the final modifier to ensure that derived classes will not change the methods and properties of the class.
On the other hand, you may want to require your class to have certain methods overridden. For example, you can create a class that provides some basic functionality but uses the abstract modifier for some methods. The implementations of the abstract methods are up to the writer of the derived class.
Version-safe modifiers, which also manage overriding, manage it from the derived-class side rather than the base-class side. Version-safe modifiers have an effect only if the base-class method they are overriding does not have inheritance modifiers.
You cannot combine two inheritance modifiers or combine an inheritance modifier with the static modifier.
Inheritance Modifier | Valid for | Meaning |
---|---|---|
Class, method, or property |
For methods or properties, this modifier indicates that the member does not have an implementation. For classes, this modifier indicates that there are one or more unimplemented methods. An abstract class or a class that contains an abstract member cannot be instantiated using the new keyword, but it can be used as a base class. |
|
Class, method, or property |
For classes that cannot be extended or methods that cannot be overridden. Using final prevents derived classes from changing the behavior of the class by overriding important functions. Methods with the final modifier can be hidden or overloaded, but not overridden. |
Version-Safe Modifiers
Version-safe modifiers help control the methods from a derived class that override methods in a base class. By using this control, you can manage whether a class you create will override methods in the base class.
By default, methods from a derived class will override methods in a base class, although inheritance modifiers in the definition of the derived class can help prevent overriding as well. Using version-safe modifiers enables you to help control whether particular methods are overridden or not.
In some situations, you may need to ensure that base-class methods are not overridden. For example, you may extend a class to change the behavior of the base-class methods. If you do not want those methods to be overridden in the base class, you can use the hide modifier for your method declarations.
On the other hand, you may want to override certain base-class methods. For example, you may want to change the methods of a class without modifying the class. By extending the class and using the override modifier for your method declarations, you can cause the new methods to override the base class.
Successful use of version-safe modifiers depends on whether or not the declaration of the base class methods used inheritance modifiers. Base-class methods marked with the final modifier cannot be overridden, and base-class methods marked with the abstract modifier cannot be hidden unless an explicit implementation for the abstract base-class method is given.
You cannot combine two version-safe modifiers or combine a version-safe modifier with the static modifier. When you are running in version-safe mode, you can use only one version-safe modifier for each method that overrides a base-class method.
expando Modifier
The expando modifier causes a class-based object to behave as if it were a JScript object. Methods and properties can be dynamically added to an expando object. For more information, see Prototype-based Objects.
You can use the expando modifier independently of the other modifiers.
Modifier | Valid for | Meaning |
---|---|---|
Class or method |
For a class, the class is given a default, indexed property that is capable of storing and retrieving dynamic properties (expandos). For a method, indicates that it is a constructor for an expando object. |
static Modifier
The static modifier signifies that a member of a class belongs to the class itself rather than to instances of the class. Consequently, class-specific data and methods may not be associated with any particular instance.
You cannot combine the static modifier with any of the version-safe or inheritance modifiers.
Modifier | Valid for | Meaning |
---|---|---|
Method, property, field, or class |
For methods, indicates that it can be called without an instance of the class. For properties and fields, designates that one copy is shared by all instances. The static modifier should not be confused with the static statement, which denotes code that initializes the class. |