Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

public abstract sealed class MyFundooClass {}

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 Apr 2012CPOL1 min read 16.4K   2   5
public abstract sealed class MyFundooClass {}

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

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

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

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

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

C#
public abstract sealed class MyAbstractClass
  {
  }

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

C#
public static class MyFundooClass
{
}

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

C#
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:

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

So, a static class in C# actually gets compiled into an abstract sealed class!. And just because you cannot create any instance of such class, the compiler won't let you 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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood Pin
Aaditya Hasiyan4-Sep-15 1:01
Aaditya Hasiyan4-Sep-15 1:01 
AnswerRe: Good Pin
Ankush Bansal9-Jan-16 6:38
Ankush Bansal9-Jan-16 6:38 
GeneralStatic class inheritance... Pin
Alexandru Lungu2-May-12 3:23
professionalAlexandru Lungu2-May-12 3:23 
GeneralRe: Static class inheritance... Pin
Ankush Bansal2-May-12 23:27
Ankush Bansal2-May-12 23:27 
Hi Alexandru,

Thanks much... appreciate that!

Thanks,
-ankush
QuestionFormatting needed Pin
Wendelius28-Apr-12 20:01
mentorWendelius28-Apr-12 20:01 

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

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