Hello readers, In this session we are going to see Sealed Class in C# with an example. A sealed class is a special class which prevents a class from being inherited by other class. You can declare a class as a sealed class when there is no need to extend it to provide additional functionality.

What is Sealed Class?

A sealed class is class which is declared using the sealed modifier. A Sealed class prevent a class from being inherited by other class. An error occurs if you try to inherit a class that is declared as a sealed class. You can declare a class as a sealed class when there is no need to extend it to provide additional functionality. Since the struct is implicitly sealed by default so if we want to prevent a class for accidental inheritance sealed modifiers should use. We can not declare an abstract class as sealed because for an abstract class it is necessary that it is inherited by other classes to extend its functionality that provides the implementation for abstract property and abstract methods. A sealed modifier can be applied to both a class or a method as well.

The syntax for declaring a Sealed class:

access-modifier sealed class class-name

Why we need a sealed class:

A sealed class is used for security reason for preventing a class from unintended derivation by which the derived class may corrupt the implementation provided in the sealed class. A sealed class is useful when you have public members in it.

In real time software projects, we use sealed class and sealed method to stop a developer to extend a class or override a method accidentally.

Example of Sealed Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public sealed class BaseClass

{
public void Display()
{
Console.WriteLine("This is a sealed class which can;t be further inherited");
}

}

public class Derived : BaseClass
{

}

class Program
{
BaseClass obj = new BaseClass();
obj.Display();
Console.ReadLine();
}

I

Sinche Base class is declared as sealed class so it will not allow inheritance.

Sealed Class

Key Points about Sealed Class:

  • A class restricts inheritance for security reason.
  • A sealed class is the last class in the class hierarchy..
  • We can declare a Sealed class as a derived class but not as a base class.
  • An abstract class cannot be declared as a sealed class because an abstract class needs to implement functionality an here we are restricting inheritance using the sealed modifier.

View More:

Conclusion:

I hope you would love this post. I would like to have your feedback, comments, and suggestions.

Thank You

Explore post tags

8 Comments

  • Hello my friend! I want to say that this post is amazing, nice written and include approximately all vital infos. I would like to see more posts like this.

  • Hey there! Someone in my Myspace group shared this site with us so I came to check it out. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Terrific blog and brilliant style and design.|

  • Unquestionably believe that which you stated. Your favorite justification seemed to be on the web the easiest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will likely be back to get more. Thanks|

  • Heya! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the fantastic work!|

  • Have you ever thought about including a little bit more than just your articles? I mean, what you say is valuable and all. However think of if you added some great pictures or video clips to give your posts more, “pop”! Your content is excellent but with pics and videos, this website could certainly be one of the best in its niche. Fantastic blog!|

  • Hello I am so delighted I found your site, I really found you by accident, while I was browsing on Bing for something else, Anyways I am here now and would just like to say thanks for a tremendous post and a all round interesting blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb job.|