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

  1. Data embody the frequencies of each of the 38 sectors over 904 spins and frequencies of the 22,527 "straight-up" bets positioned. Summary data can also be|can 퍼스트카지노 be} out there on how bettors react to streaks in even-money outcomes—allowing one to check for the gambler's fallacy. The case is used in an introductory course on data evaluation.

    ReplyDelete

Post a Comment

Popular posts from this blog

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.

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  Vide...

How to get IOS 7 look and feel on Android

Get IOS 7 like Control Center on Android Phone with the help of an App One of the cool features of IOS 7 is its Control Center, this allows users to access quick settings like airplane mode, vibration mode, WiFi, Bluetooth, Brightness Control etc. To access Control Center user has to swipe-up the screen from bottom.  Now Android users can also experience this handy feature and get the look and feel of IOS 7 on their Android Devices with the help of a new app called  Control Center . This App was launched recently and in the beginning  it faced Copyright issues with Apple but now it is available for download on Play Store .  Control Center allow us to customize the settings of the app according to our choice. We can change the order of toggles, add app shortcuts of our choice, Configure Touchable Area etc. This app makes the android device very handy and quick. Sometimes while using another app if we want to change the brightness of the screen or...