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()
{
Console.WriteLine("A.Display1");
}
protected virtual void Display2()
{
Console.WriteLine("A.Display2");
}
}

class B : A    {        
sealed protected override void Display1()        
{            
Console.WriteLine("B.Display1");        
}        
protected override void Display2()        
{            
Console.WriteLine("B.Display2");        
}    
}    

class C : B    {        
//protected override void Display1() //Compile Error        
//{        
//     Console.WriteLine("A.Display1");        
//} 
protected override void Display2()        
{            
Console.WriteLine("A.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 :)

Comments

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.  YouTube.com  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.