Few Ways to Prevent Instantiation of Class

Let's explore some very interesting facts about OOP. There are a few ways prevent instantiation of a class.

Let's proceed to dig into this so that my geeks can have an idea of this.

Those ways are:

  1. Abstract
  2. Static Class
  3. Private and protected constructor

Now jump to Abstract classes first.

1. Abstract

An abstract class is the one that is not used to create objects. An abstract class is designed to act as a base class (to be inherited by other classes). Some important facts about abstract are the following:

  • An abstract class cannot be a sealed class or static.
  • Declarations of abstract methods are only allowed in abstract classes.
  • An abstract method cannot be private.
  • The access modifier of the abstract method should be the same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

We declare a class as abstract to prevent us from creating an object of the class such as in the following example:

Instantiation-of-class.jpg

It states that "Error 1 Cannot create an instance of the abstract class or interface "ClassInstantiation.StopInstantiation".

Now for static classes.

2. Static Class

A class can be declared static, indicating that it contains only static members. It is not possible to create an instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated with a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:

  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors

If you try to create an instance of a static class then it also prompts you with an error after implementation of the code as in the following:

Instantiation-of-class1.jpg


Whenever you try to create an instance, it prompts you with the following error:

Instantiation-of-class2.jpg

3. Private or protected constructor

If we declare a private or protected constructor then it also prevents us from creating an instance of the class as the following code shows:

Instantiation-of-class3.jpg

These are the easiest ways to prevent class instantiation, although for some techies it may be very easy but it's also a very frequent question and most desirable question to be asked in interviews.

So I thought to share in a more practical approach. It's very easy to understand, especially for those unfamiliar with this. I hope you enjoyed this demonstration.

A sample application has been attached with all combinations. Kindly comment and uncomment code as needed to understand the functionality.

Enjoy Coding.

Up Next
    Ebook Download
    View all
    Learn
    View all