Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

public abstract sealed class MyFundooClass {}

, 30 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Ever heard of a class that is abstract as well as sealed?Sounds impossible right? abstract means some functions must be defined in derived class and sealed means class can not be derived. Conflicting statements!!?Well actually the catch is in the meaning of abstract class. Creating abstract class me

Ever heard of a class that is abstract as well as sealed? Sounds impossible right? abstract means some functions must be defined in derived class and sealed means class can not be derived. Conflicting statements!!? Well actually the catch is in the meaning of abstract class. Creating abstract class means that instance of a class can not be created. That is why you can have following class in C#:

 public abstract class MyFundooClass  
   {  
     //abstract class without any abstract members.      
   }

So now you are clear that abstract means class can not be instantiated. But what if someone decided to derive your class and create an instance of it? For ex:

 public class MyFundooClassImpl : MyFundooClass  
   {  
   }
 void SomeFunc()  
 {  
   MyFundooClass obj= new MyFundooClassImpl(); //Ohh some has an instance of MyFundooClass !!??  
 }

Here the sealed keyword comes to rescue. Sealed means class can not be derived. So now you can write your class as

 public abstract sealed class MyAbstractClass  
   {  
   }

Now, no one can derive your class, and no one can create any instance of it. Happy!! But question is why would any one want to create a class that is abstract sealed? And the answer is, ever heard of static class?

 public static class MyFundooClass  
 {  
 }

The static class in C# are equivalent to abstract sealed class. In fact C# compiler won't let you write:

 public abstract sealed class MyAbstractClass  //Error!!!
   {  
   }

But if you compile a static class, and investigate the IL, you would be surprised to see that IL is

.class public abstract auto ansi sealed beforefieldinit C_Sharp_ConsoleApp.MyFundooClass  
     extends [mscorlib]System.Object  
 {  
 }

So a static class in C# actually gets compiled in to a abstract sealed class!. And just because you can not create any instance of such class compiler won't let to define any instance member, so you must have only static members in such class.

Happy coding!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ankush Bansal
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralStatic class inheritance... PinmemberAlexandru Lungu2-May-12 4:23 
GeneralRe: Static class inheritance... PinmemberAnkush Bansal3-May-12 0:27 
QuestionFormatting needed PinmvpMika Wendelius28-Apr-12 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by Ankush Bansal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid