A Sealed Class

Introduction

A sealed class is a class that cannot serve as a base class in inheritance. That is, you cannot derive a class from a sealed class.

Static Classes

As seen previously, a static class is a class marked with the static keyword and whose all members are static. Here is an example of a static class:

public static class Calculations
{
    public static double Add(double a, double b)
    {
        return a + b;
    }

    public static double Add(double a, double b, double c, double d, double e)
    {
        return a + b + c + d + e;
    }

    public static double Subtract(double a, double b)
    {
        return a - b;
    }

    public static double Multiply(double a, double b)
    {
        return a * b;
    }
}

When a class has been made static, no class can be derived from it. This means that when you create a static class, it becomes automatically sealed.

Sealing a Class

A regular class, that is, a non-static class, can be sealed so it would not act as a base class for another class. To get a sealed class, you use a keyword named sealed. Therefore, to seal a class, type the sealed keyword to the left of the class keyword.

If the class is marked with an access modifier, the sealed keyword can appear before or after the access modifier. Here are examples:

sealed public class TimeWorked
{

}

public sealed class WorkingTime
{

}

A class that is derived from another can also be sealed. Here is an example:

public abstract class Triangle
{
}

sealed public class Irregular : Triangle
{
}

Remember that once a class is sealed, it cannot serve as a parent of another class. As an alternative, the class can be used to create a property in another class that would use it as a pseudo-parent.

Characteristics of a Sealed Class

A Sealed Method

If you use the sealed keyword on a class, the whole class becomes sealed, but you may not want the whole class to be sealed. Sometimes, you may want only some members to be sealed.

One of the charateristics of inheritance is that a derived class can provide a behavior of a parent's method. This can be done by overriding a method of the parent. Sometimes when creating a non-sealed class, you may want to prevent the deriving class(es) from overriding a certain method. In this case, you can seal the method. A sealed method is a function that doesn't allow deriving classes to override it.

If you create a new method in a derived class, that is, a method that does not exist in the parent class, you cannot seal it. This means that you can seal only a method that can be overridden. Therefore, before sealing a method, you must first create it in class. You must mark that method as abstract or virtual.

Remember that, in a derived class, you must override every parent's abstract or virtual method. To seal a method, in the derived class, precede the return type by the sealed keyword. The sealed keyword can appear before or after override. Here are examples:

public abstract class RoundShape
{
    public virtual  double Radius { get; set; }
    public abstract double Diameter { get; }
}

public class Circle : RoundShape
{
    private double rad;

    public sealed override double Radius // sealed before override
    {
        get
        {
            return this.rad;
        }

        set
        {
            this.rad = value;
        }
    }

    public override sealed double Diameter // override before sealed = same thing
    {
        get
        {
            return this.rad * 2;
        }
    }
}

A Sealed Property

A property from a class A is said to be sealed if no class B deriving from class A is allowed to provide a new version of the property.

As seen for a method, before creating a sealed property, its class must be derived from another class. Here is an example of such as class created as abstract:

public abstract class RoundShape
{
    public virtual  double Radius { get; set; }
    public abstract double Diameter { get; }
}

Of course, you must derive a class from such as class. Before sealing a property, you must override it from the parent class. That is, you must mark the property in the derived class as override. To seal a property, type the sealed keyword close to the override keyword. The sealed keyword can appear before or after override.


Previous Copyright © 2002-2022, FunctionX Friday 22 October 2021 Next