What is called sealed classes?Can we derive sealed classes?
Sealed Classes <o:p></o:p>
Sealed is another modifier that applies to classes. aaa is a sealed class. No class can derive from aaa. In another words aaa cannot act as a base class for any class.<o:p></o:p>
a.cs<o:p></o:p>
public class zzz<o:p></o:p>
{<o:p></o:p>
public static void Main()<o:p></o:p>
{<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
sealed class aaa<o:p></o:p>
{<o:p></o:p>
}<o:p></o:p>
class bbb : aaa<o:p></o:p>
{<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
Compiler Error<o:p></o:p>
a.cs(10,7): error CS0509: �bbb� : cannot inherit from sealed class �aaa�<o:p></o:p>
<o:p></o:p>
a.cs<o:p></o:p>
public class zzz <o:p></o:p>
{<o:p></o:p>
public static void Main() <o:p></o:p>
{<o:p></o:p>
aaa a = new aaa();<o:p></o:p>
System.Console.WriteLine(a.i);<o:p></o:p>
a.abc();<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
sealed class aaa<o:p></o:p>
{<o:p></o:p>
public int i = 9;<o:p></o:p>
public void abc()<o:p></o:p>
{<o:p></o:p>
System.Console.WriteLine(�hi�);<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
Output<o:p></o:p>
9<o:p></o:p>
hi<o:p></o:p>
<o:p></o:p>
The only difference between a sealed class and a non-sealed class is that a sealed class cannot be derived from. Otherwise there is no difference at all. It can contain the same variables, functions etc as a normal class does . A sealed class lets us create classes which no one can derive from. Thus the code in such classes cannot be overridden. Also as the compiler knows this, certain run time optimizations can be performed on a sealed class
Regards,
Dnyaneshwar Parkhe
http://dnyaneshwar.blog-city.com