Skip to main content

Sealed Classes and Sealed Methods

Sealed Class

Sealed class is a special kind of class in C# which is used to prevent the inheritance to next level. Any class with sealed keyword cannot be derived to any subclass. We will get a compile error if we try to define base class as sealed class.

sealed class A { }
class B: A { } //CompileError

In the above example if we see, Class B tries to inherit from class A which is sealed. We will get a compiler error.

Now if we move the sealed keyword from Class A to Class B and try to inherit from class B we will get a compile error ('C': cannot derive from sealed type 'B').

class A { }
sealed class B: A { }
class C : B { } //Compile Error

Below code should be fine, as Sealed class should be the last in the inheritance hierarchy.

class A { }
class B : A { }
sealed class C : B { }

  • Sealed and Abstract modifiers cannot be used together as Abstract Class has to provide the functionality and Sealed will prevent that.
  • Sealed class cannot be a base class, it can only be the last derived class in the hierarchy.
  • Structs are implicitly sealed.
  • Sealed class provides performance benefits also, it depends on execution environments.

Sealed Methods

We can use sealed modifier on methods or properties also which override a virtual method or property of a base class. Sealed keyword helps us to allow classes to derive from our class and prevent them from overriding the virtual methods/properties.

In the below example if we see, Class A has 2 virtual methods Display1 and Display2. Class B inherits from Class A, in Class B we have sealed Display1 method as we don't want this method to be overridden in the deriving class. Now Class C inherits from Class B, if we try to override Display1 method of Class B, we get a compile error saying ( 'C.Display1()': cannot override inherited member 'B.Display1()' because it is sealed) but we can still override Display2 method as we didn't Seal it.

class A {
protected virtual void Display1()
protected virtual void Display2()

class B : A    {        
sealed protected override void Display1()        
protected override void Display2()        

class C : B    {        
//protected override void Display1() //Compile Error        
//     Console.WriteLine("A.Display1");        
protected override void Display2()        

  • Sealed keyword is always used with Override keyword.
  • Sealed method is not used with Sealed Class.
  • Sealed method is used to define the overriding level of a virtual method.
Hope this article helps you in understanding the concept of Sealed identifier.
For any queries and suggestions, comment below :)


Post a comment

Popular posts from this blog

How to remove Shortcut Virus from Pendrive/Memory Card without Antivirus

Remove Shortcut Virus from Pen Drive and Memory Cards without Antivirus Many of us had faced or facing this Shortcut Virus issue with our Pen Drives and Memory Cards. Compare to other flash drives like memory cards,external hard drives etc, we face this problem more with pendrives because we use Pendrives a lot so there are more chances of getting affected with Shortcut Virus.  All the folders and files in the Pendrivegets converted to Shortcuts and you are unable to access your files. To get rid of this issue we simply format our Pendrive. But sometimes we have important data in our Pendrive and we don't want to lose that data, in that case we can follow below steps to get rid of SHORCUT VIRUS without any Antivirus software. Insert your infected Pendrive and follow the steps.. Contents of infected Pendrive(Before) Step 1 : Open Command Prompt. (Press Win key + R and type cmd and click on OK) Step 2 : Now type attrib -h -r -s /s /d g:\*.* and press Ent

How to Download YouTube Videos on Android Device

How to Download YouTube Videos on Smart Devices We all know that YouTube is the best video-sharing website. More than 100 hours of Video are uploaded to YouTube every minute.  owns a Global Rank # 3. Since there has been a huge development in Smartphones, YouTube has launched app for Smartphones and other smart devices like Tablets, ipads, ipods etc Operating on different Operating Systems like Android, IOS, Blackberry etc. YouTube app is very handy and easy to use, and gets updates every now and then. Mobile make almost 40% of YouTube's global watch time. There’s so much great content on YouTube it’s easy to find videos you want to watch again and again. When you come across any crazy video, you might want to download it so you don’t have to rely on the internet to watch it next time. Here’s how to download a video from YouTube on Android with the help of an app called Videoder . Videoder- video downloader Step 1 : Open Play Store and download  Videoder  app.

How to attach an Email to another Email in Outlook

How to attach an Email to another Email in Outlook Sometimes you want to attach an email to another email as proof to your current mail, in this situation you just cant copy and paste the mail chain in the mail body. Lets see the correct way of doing that. Click on new mail, new window will be opened/Reply to the existing mail In the tool bar click on message tab, under it you will find attach file, attach item, signature etc.  Click on attach item, from the drop down menu select Outlook item. A dialog box will appear, in that browse and select the mail which you want to attach.  You can even attach business card, for that in 3rd step select attach Business Card instead of Outlook item.