sealed class contain a concrete methods.but it does not participate in inheritance why?

Posted by Harieswer under C# on 7/28/2015 | Points: 10 | Views : 1167 | Status : [Member] | Replies : 3
sealed class contain a concrete methods.but it does not participate in inheritance why?
once it used in inheritance ,it stop the inheritance why?




Responses

Posted by: Sheonarayan on: 8/18/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
The sealed keyword is used to stop inheritance in C#. When you have marked class as sealed it means that you do not want any method to be inherited in the derived class. This is how sealed behave and functionality is written.

Why? I think I do not have answer but this is how the behavior is written for sealed modifier in C#.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Harieswer, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shreedar on: 8/19/2015 [Member] Starter | Points: 25

Up
0
Down
If we want to avoid a class i.e., parent class from inheriting, then we declare that class as sealed class.

It is recommended to declare a class as sealed class, if we are not having implementation of that class at any other place.

Where as by declaring the method as sealed and class as public we can stop method from overriding but class can be inherited.

Sealed keyword has a code predefined which will not allow inheriting.

Regards

Sridhar Thota.


Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

Harieswer, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
It is because of design choice made by the .Net CLR team.
However, if a class is Sealed then .NET doesn’t need to consult the virtual-function-call table to find the actual type, since it knows that no derived type can exist. Therefore, it could emit a ‘call’ instead of ‘callvirt’ or at least optimise the machine code, thus producing a performance benefit.

--
Thanks & Regards,
RNA Team

Harieswer, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response