3

i got a sample code for sealed abstract class. i just do know what is the use of sealed abstract class. whatever is abstract that has to be extended and sealed keyword prevent to extend the class. so when class is sealed & abstract then it can be extended and also instantiated. so what will be the real use of sealed abstract class?

some one told me it will solve the purpose of static class. if possible please discuss the use of sealed abstract class with few sample code and scenario when a class is required to design as a sealed abstract class.

sealed abstract class BaseBook : IBook
{
private string _title;

public virtual string Author
{
    get
    {
        Console.WriteLine("Base book GET!");
        return _title;
    }
    set
    {
        Console.WriteLine("Base book SET!");
        _title = value;
    }
}

public string Title
{
    get;
    set;
}
}

thanks

David M
  • 71,481
  • 13
  • 158
  • 186
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 5
    You can't have an sealed abstract class in c# – Brunner May 15 '12 at 14:29
  • 3
    This doesn't compile. [CS0418 - an abstract class cannot be sealed or static](http://msdn.microsoft.com/en-us/library/32twkh69%28v=vs.90%29.aspx). – AakashM May 15 '12 at 14:30
  • 31
    One possible use case is to prevent the code from compiling – Muhammad Hasan Khan May 15 '12 at 14:31
  • Where did you get this sample code? – Paul Sasik May 15 '12 at 14:31
  • 6
    Hey guys, it's a valid question. If the OP's been given a code sample saying this, don't downvote **him** or vote to close. A straight and simple answer is enough. – David M May 15 '12 at 14:32
  • A class can't be sealed and abstract at same time. Edited your question title. – Rahul May 15 '12 at 14:32
  • @HasanKhan You just made me piss myself at work. Dammit, man. Now I have to go home and change my pants. Funniest. Comment. Ever. – Yatrix May 15 '12 at 14:32
  • 2
    @CodeCaster - no, you're (deliberately?) misinterpreting me. There is no possible use for `sealed abstract`, as others have said it clearly won't compile, and its use here even confused the OP - so he's asking here for clarification. All of us who have jumped in here know the answer is that the code sample he's given is b****cks, but that doesn't mean his question deserves to be treated like this does it? – David M May 15 '12 at 14:35
  • 3
    I think the OP is getting downvoted because this is so easy to test: `sealed abstract class A {}` = `Compiler error: An abstract class cannot be sealed or static.` An SOer with 1.8k+ rep should know better. – Paul Sasik May 15 '12 at 14:36
  • @Paul - yes, agreed. But has anyone actually said to him "have you tried compiling it?" I guess Aakash has said it _won't_ compile... – David M May 15 '12 at 14:37
  • 1
    @DavidM I think it's getting dinged for lack of research. This is also not really a programming question, more so a topic for discussion which belongs on http://programmers.stackexchange.com more than here. That's just my opinion. – Yatrix May 15 '12 at 14:38
  • 1
    @Yatrix - I take the point, but let's be honest, Googling for use of abstract with sealed isn't going to turn much up either... In the absence of easy answers with that level of research, **many** turn to SO for help. – David M May 15 '12 at 14:39
  • 5
    @DavidM I guess for me "trying to compile it" is well within the *minimum we can reasonably expect* of askers in possession of a code sample and wondering what it does. Isn't it? – AakashM May 15 '12 at 14:42
  • @DavidM I don't blame him for asking. I'm just explaining why folks are down-voting. I've had a few downvotes in my day. =) – Yatrix May 15 '12 at 14:43
  • I could see a use for it in combination with code contracts on interfaces where you dont want your class containing only contracts from being used in any way. – Polity May 15 '12 at 14:43
  • 2
    @Aakash - yes, I do take your point too, of course! But look at his question on face value - he's been given a code sample, it looks counterintuitive, it doesn't even compile, but he's curious. He can't find anything helpful by Googling. So he turns to SO - is that so unreasonable? – David M May 15 '12 at 14:45
  • Oh, and @Hasan - what Yatrix said first! :) – David M May 15 '12 at 14:50
  • Its a valid question (may be not for todays C#), and a duplicate here: http://stackoverflow.com/questions/1268983/why-declare-static-classes-as-sealed-and-abstract-in-c – nawfal May 04 '13 at 12:40

5 Answers5

9

This code won't compile. A class cannot be abstract and sealed. This doesn't make any sense:

CS0418 - 'BaseBook': an abstract class cannot be sealed or static

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • You're right that it won't compile. But `sealed abstract` classes make sense if you don't want to create instances or derive from it, the conventional name for such classes is `static` classes. – CodesInChaos May 15 '12 at 14:32
  • Why would you make the class abstract if you don't want to derive from it? Agreed about the static classes. – Darin Dimitrov May 15 '12 at 14:33
  • 4
    My point is that `static` classes in C# map to `sealed abstract` in IL. – CodesInChaos May 15 '12 at 14:34
7

MSDN: It is not permitted to use the abstract modifier with a sealed class.

Link: http://msdn.microsoft.com/en-us/library/88c54tsw%28v=vs.71%29.aspx

Hope that answers your question.

Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
5

C# will not allow you to have a class that is both sealed and abstract. It won't compile. For a static class (one with only static methods), mark it as static to prevent inadvertent instantiation.

afeygin
  • 1,213
  • 11
  • 26
2

Simply there is no use as it does not exist.

daryal
  • 14,643
  • 4
  • 38
  • 54
2

First, you can't declare a sealed abstract class in C#, it's illegal

so when class is sealed & abstract then it can be extended and also instantiated

No, on the contrary; it can neither be inherited nor instantiated...

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758