New Message on dotNET User Group Hyd

Question Of The Day !!!!

Reply
  Reply to Sender   Recommend Message 6 in Discussion
From: dnyanesh

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

 

 


Yahoo! Mail
Stay connected, organized, and protected. Take the tour

View other groups in this category.

Click Here
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to