Sealed


Sealed classes are the reverse of abstract classes. Abstract types must be inherited. You cannot create an instance of an abstract type. Conversely, sealed types cannot be inherited and are concrete. Sealed classes cannot be refined by a derived type. Sealed classes are terminating nodes in the class hierarchy.

The sealed modifier can also be applied to instance methods, properties, events, and indexers. It cannot be applied to static members. Sealed members are allowed in sealed and nonsealed classes. A sealed member must override a virtual or implied virtual member, such as an abstract member. However, a sealed member itself cannot be overridden. It is sealed. The sealed modifier must be combined with the override modifier. Although sealed members cannot be overridden, a sealed member in a base class can be hidden in a derived type with the new modifier. As a bonus, the CLR can perform optimizations on sealed members.

The following code demonstrates both a sealed class and a sealed member. In this example, the HourlyEmployee class cannot be further refined. In addition, the HourlyEmployee.Pay method cannot be overridden.

 public abstract class Employee {     public virtual void Pay() {     }     public abstract void CalculatePay(); } public sealed class HourlyEmployee: Employee {     public sealed override void Pay() {         CalculatePay();     }     public override void CalculatePay() {     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net