SIGN UP MEMBER LOGIN:    
ARTICLE

Sealed Classes in C#

Posted by Mahesh Chand Articles | C# Language September 23, 2002
In this article, I will discuss how to create and use sealed classes. I will also show you where programming gurus use sealed classes in their real world applications.
Reader Level:


In this article, I will discuss how to create and use sealed classes in C# and .NET. We will also review why programming gurus use sealed classes in their real world applications. 

Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited.
In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET, NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class, compiler throws an error.

If you have ever noticed, structs are sealed. You cannot derive a class from a struct. 

The following class definition defines a sealed class in C#:


// Sealed class

sealed class SealedClass

{

    }

In the following code, I create a sealed class SealedClass and use it from Class1. If you run this code, it will work fine. But if you try to derive a class from sealed class, you will get an error.



using System;

class Class1

{

    static void Main(string[] args)

    {

        SealedClass sealedCls = new SealedClass();

        int total = sealedCls.Add(4, 5);

        Console.WriteLine("Total = " + total.ToString());

    }

}

// Sealed class

sealed class SealedClass

{

    public int Add(int x, int y)

    {

        return x + y;

    }

} 


Why Sealed Classes?


We just saw how to create and use a sealed class. The main purpose of a sealed class to take away the inheritance feature from the user so they cannot derive a class from a sealed class. One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.
The Pens class represent the pens for standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.
So when you're designing your application, you may keep in mind that you have sealed classes to seal user's boundaries.
In the next article of this series, I will discuss some usage of abstract classes.


Note: At the time of writing this article, C# language did not have static classes. Now C# has static classes.

Login to add your contents and source code to this article
share this article :
post comment
 

How is it going to be advantage while making a class as sealed if it has all the members as static Could you please throw some light on this.

Posted by Prashant Jain Dec 29, 2011

need more expl dude !!!

Posted by jhn Apr 25, 2011

This is wonderfull exampled for sealed class

Posted by Govindaraj Krishnan Apr 18, 2011

Yeah, you may not be able to derive or inherit a sealed class, but I can still do this: sealed class Data { public static string TestClass() { return "testdata"; } } Response.Write(Data.TestClass()); But if I get rid of the "static" keyword from the TestClass() method, I can do this: Response.Write(new Data().TestClass()); So, if I don't want someone developing a presentation layer to be able to access to the Data namespace, it seems like neither "sealed" nor "static" accomplishes this. I guess I'm still confused as to why I'd ever want to use these when, obviously, they're just as accessable as a non-static or non-sealed class.

Posted by Jeremy Mar 30, 2011

What are you trying to do? Purpose of sealed and virtual is totally different.

Posted by Mahesh Chand Nov 07, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor