{"id":173,"date":"2014-08-13T11:56:16","date_gmt":"2014-08-13T11:56:16","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=173"},"modified":"2014-08-13T11:56:16","modified_gmt":"2014-08-13T11:56:16","slug":"sealedstatic-and-abstract-class-difference","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=173","title":{"rendered":"Sealed,static and abstract class difference."},"content":{"rendered":"<p><b>Sealed Class<\/b><\/p>\n<p>A sealed class cannot be used as a base class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.<\/p>\n<p>&nbsp;<\/p>\n<p>We can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables to allow classes to derive from class and prevent them from overriding specific virtual methods or properties.<\/p>\n<p>class X<\/p>\n<p>{<\/p>\n<p>protected\u00a0virtual\u00a0void F() { Console.WriteLine(&#8220;X.F&#8221;); }<\/p>\n<p>protected\u00a0virtual\u00a0void F2() { Console.WriteLine(&#8220;X.F2&#8221;); }<\/p>\n<p>}<\/p>\n<p>class Y : X<\/p>\n<p>{<\/p>\n<p>sealed\u00a0protected\u00a0override\u00a0void F() { Console.WriteLine(&#8220;Y.F&#8221;); }<\/p>\n<p>protected\u00a0override\u00a0void F2() { Console.WriteLine(&#8220;Y.F2&#8221;); }<\/p>\n<p>}<\/p>\n<p>class Z : Y<\/p>\n<p>{<\/p>\n<p>\/\/ Attempting to override F causes compiler error CS0239.<\/p>\n<p>\/\/ protected override void F() { Console.WriteLine(&#8220;C.F&#8221;); }<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Overriding F2 is allowed.<\/p>\n<p>protected\u00a0override\u00a0void F2() { Console.WriteLine(&#8220;Z.F2&#8221;); }<\/p>\n<p>}<\/p>\n<p>It is an error to use the <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/sf985hc5.aspx\">abstract<\/a> modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.<\/p>\n<p>When applied to a method or property, the sealed modifier must always be used with <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/ebca9ah3.aspx\">override<\/a>.<\/p>\n<p>Because structs are implicitly sealed, they cannot be inherited.<\/p>\n<p><b>Static Class<\/b><\/p>\n<p>A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Difference between static and sealed class.<\/b><\/p>\n<p><b>Sealed classes: <\/b><\/p>\n<p>1)Can create instances, but cannot inherit<\/p>\n<p>2)Can contain static as well as nonstatic members.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Static classes:<\/b><\/p>\n<p>1)Can neither create their instances, nor inherit them<\/p>\n<p>2)Can have static members only.<\/p>\n<p><strong>\u00a0Difference between abstract and sealed<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/sf985hc5.aspx\">abstract<\/a> keyword enables to create classes and <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/0b0thckt.aspx\">class<\/a> members that are incomplete and must be implemented in a derived class.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/88c54tsw.aspx\">sealed<\/a> keyword enables to prevent the inheritance of a class or certain class members that were previously marked <a href=\"http:\/\/msdn.microsoft.com\/en-IN\/library\/9fkccyh4.aspx\">virtual<\/a>.<\/p>\n<p>class Animal<\/p>\n<p>{<\/p>\n<p>public void Eat() { Console.WriteLine(&#8220;Eating.&#8221;); }<\/p>\n<p>public override string ToString()<\/p>\n<p>{<\/p>\n<p>return &#8220;I am an animal.&#8221;;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>sealed class fr:Animal<\/p>\n<p>{<\/p>\n<p>public static void hj(int h) { h = 5; }<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>static class fr1:Animal&#8212;&#8211;<strong>Error<\/strong><\/p>\n<p>{<\/p>\n<p>public static void hj(int h) { h = 5; }<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Sealed Class A sealed class cannot be used as a base class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. &nbsp; We can also use the sealed modifier on a method or property that overrides a virtual method or&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=173\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-c-vb","category-oop","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=173"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/173\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}