Sealed Classes

Sealed classes are the reverse of abstract classes. While abstract classes are inherited and refined in the derived type, sealed classes cannot be inherited. A sealed class cannot be refined any further. An abstract class is "abstract" (which perhaps goes without saying), while a sealed class is concrete. You can create instances of a sealed class. Sealed classes are terminating nodes in a class hierarchy, while an abstract class is typically near the top of a class hierarchy. A sealed class is used to prevent further refinement through inheritance. For example, I am a developer of a class library. Some of the classes in the class library are extensible. However, other classes are intended to be used "as is." Those classes are marked as sealed.

The sealed modifier also can be used with instance methods, properties, events, and indexers. Static members do not support the sealed modifier. A sealed member can override a virtual member, such as a virtual or abstract member. However, a sealed member itself cannot be overridden, but a sealed member can be hidden in a derived type with the new modifier.

The following code demonstrates a sealed member. In this example, the HourlyEmployee.Pay method cannot be overridden:

public abstract class Employee {
    public virtual void Pay() {
    }

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

    public override void CalculatePay() {
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.129.90