ARTICLE

What are sealed classes and sealed methods

Posted by Puran Mehra Articles | Visual C# May 25, 2009
In this article, I will try to explain sealed classes and sealed methods in a very simple way in C# language.
Reader Level:

What is sealed class and sealed method?

 

In this article I will try to explain sealed class and sealed method in a very simple way.

 

Sealed Class

 

Sealed class is used to define the inheritance level of a class.

 

The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class.

 

Some points to remember:  

1.  A class, which restricts inheritance for security reason is declared, sealed class.
2.  Sealed class is the last class in the hierarchy.
3.  Sealed class can be a derived class but can't be a base class.
4.  A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are
     restricting it to inherit.

Practical demonstration of sealed class

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace sealed_class

{

    class Program

    {

        public sealed class BaseClass

        {

            public void Display()

        {

            Console.WriteLine("This is a sealed class which can;t be further inherited");

        }

    }

 

        public class Derived : BaseClass

        {

            // this Derived class can;t inherit BaseClass because it is sealed

        }

   

        static void Main(string[] args)

        {

            BaseClass obj = new BaseClass();

 

            obj.Display();

 

            Console.ReadLine();

        }

    }

}

 

Sealed Methods

 

Sealed method is used to define the overriding level of a virtual method.

 

Sealed keyword is always used with override keyword.

 

Practical demonstration of sealed method

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace sealed_method

{

    class Program

    {

        public class BaseClass

        {

           

            public virtual void Display()

            {

                Console.WriteLine("Virtual method");

            }

        }

 

       public class DerivedClass : BaseClass

        {

            // Now the display method have been sealed and can;t be overridden

            public override sealed void Display()

            {

                Console.WriteLine("Sealed method");

            }

        }

 

       //public class ThirdClass : DerivedClass

       //{

 

       //    public override void Display()

       //    {

       //        Console.WriteLine("Here we try again to override display method which is not possible and will give error");

       //    }

       //}

 

        static void Main(string[] args)

        {

 

            DerivedClass ob1 = new DerivedClass();

            ob1.Display();

 

            Console.ReadLine();

        }

    }

}

 

Hope this article will give you better view of sealed class and sealed method. Waiting! for your valuable feedback.

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

It is easy to understand. Good job. Keep it up.....

Posted by Maharajan Natarajan Mar 05, 2010

Thanks for your prompt reply

Posted by AnthonyBenedict P Jun 19, 2009

Dear Anthony,

 

Here is your answer to the queries:

 

What is the advantage to use sealed classes and in what kind of situation we should prefer to use sealed classes?

 

Sealed class is used to define the inheritance level of a class. If you don’t want a class to be inherited further you declare the class as seal.

 

In a way you are stopping access to the class, which is sealed. Here the concept of OOPs comes. Exposing those features, which are required. Sealed class, restricts inheritance for security reasons.

 

Sealed class can be a derived class but can't be a base class.

 

Base class is the parent class of a derived class. Classes may be used to create other classes. A class that is used to create (or derive) another class is called the base class. The class, which is derived, is called derived class.

 

Sealed class can be a derived class but can't be a base class. Sealed class is the last class in the hierarchy. An error occurs if a sealed class is specified as the base class of another class.

Posted by Puran Mehra Jun 19, 2009

What is the advantage to use sealed classes and in what kinda situation we should prefer to use sealed classes?


Second question is, u wrote in this article "Sealed class can be a derived class but can't be a base class.
What is the difference between a derived class and base class?

Posted by AnthonyBenedict P Jun 19, 2009

Thanks Gopinathbabu.

Posted by Puran Mehra Jun 09, 2009
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts