Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to make a class Sealed without Sealed Keyword

0.00/5 (No votes)
10 Aug 2006 1  
We can make a class sealed without sealed keyword

Sample Image - Class_Without_Sealed.jpg

Introduction

An article on how to make a class sealed without using Sealed keyword. The included project includes two ways to make a class sealed using Singleton Pattern and Without singleton pattern.

The class SealedClassSingleton.cs follows a singleton pattern and instance of this class allows to invoke methods of this class.

Another workaround is not to follow singleton pattern and have all static methods and methods can be invoked by the classname as defined in the class SealedClass.cs.

The purpose of above two classes is to make them sealed without using Sealed keyword.

Using the Code

The console application contains an entry point class named clsMain.cs and the Main method in it contains two code blocks to invoke methods of two different sealed classes.

The following code defined in class DerivedClass generates compile time errors when we try to inherit from SealedClassSingleton or SealedClass classes.
<code>

public class DerivedClass:SealedClassSingleton //This class is commented as it gives compilation error

{

public DerivedClass()

{

//

// TODO: Add constructor logic here

//

}

}

</code>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here