Home

Sealed Classes and Sealed Members

 

Sealed Classes

By default, any class you create can be used as a base class for another class. That is, another class can be derived from any other class you create. If you want to prevent a class from serving as a base for another class, you can flag that class as sealed.

To mark a class as sealed, type the sealed keyword after its name. Here is an example:

using namespace System;

public enum class FlowerColor
{
    Red = 1,
    White,
    Pink,
    Yellow,
    Blue,
    Orange,
    Lavender,
    Multiple,
    Other
};

public enum class FlowerArrangement
{
    Basket  = L'A',
    Bouquet = L'U',
    Vase    = L'V',
    Bundle  = L'D',
    Any
};

public ref class CFlower sealed
{
public:
    String ^ Type;
    FlowerColor Color;
    FlowerArrangement Arrangement;
    double UnitPrice;

    CFlower(String ^ type, FlowerColor clr,
	    FlowerArrangement arng, double price);
};

CFlower::CFlower(String ^ type, FlowerColor clr,
	         FlowerArrangement arng, double price)
    : Type(type),
      Color(clr),
      Arrangement(arng),
      UnitPrice(price)
{
}

void ShowFlower(const CFlower ^ one)
{
    Console::WriteLine("== Flower Order ==");
    Console::WriteLine(L"Flower Type:  {0}", one->Type);
    Console::Write(L"Flower Color: ");
    Console::WriteLine(one->Color);
    Console::Write(L"Arrangement:  ");
    Console::WriteLine(one->Arrangement);
    Console::WriteLine(L"Price:        {0:C}", one->UnitPrice);
}

int main()
{
    CFlower ^ inspiration = gcnew CFlower(L"Roses", FlowerColor::Pink,
	                                  FlowerArrangement::Bouquet, 32.25);
	
    ShowFlower(inspiration);

    Console::WriteLine();
    return 0;
}

After creating a sealed class, remember that you cannot derive any class from it. If you do, you would receive an error.

Sealed Properties and Methods

Instead of sealing a whole class, you may want only one or some of its members to be sealed. Such a class should allow derivation. That is, the class should not be marked as sealed. When creating the class, you can define what property or method must not be overridden by derived classes. This means that you can prevent derived classes from providing new versions of designated properties and methods.

To mark a member as sealed, it must first be flagged with virtual. Then, after the closing parentheses of the method, the get() or set() methods of a property, type the sealed keyword. Here is an example:

public ref class CCircle
{
private:
    double rad;

public:
    property double Radius
    {
	virtual double get() sealed { return rad; }
	virtual void set(double r) sealed
	{
	    if( r <= 0 )
		rad = 0;
	    else
		rad = r;
	}
   }

    virtual property double Circumference
    {
	double get() { return Radius * 2 * Math::PI; }
    }

    virtual double CalculateArea();
    virtual void Show();

    virtual ~CCircle();
};

double CCircle::CalculateArea()
{
    return Radius * Radius * 3.14159;
}

void CCircle::Show()
{
    Console::WriteLine(L"Circle Characteristics");
    Console::WriteLine(L"Radius:        {0}", Radius);
    Console::WriteLine(L"Circumference: {0}", Circumference);
    Console::WriteLine(L"Area:          {0}", CalculateArea());
}

When a property or method of a class has been marked as sealed, you cannot override it in a derived class. If you do, you would receive an error.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next