views:

579

answers:

9

I was just wondering, since the sealed keyword's existence indicates that it's the class author's decision as to whether other classes are allowed to inherit from it, why aren't classes sealed by default, with some keyword to mark them explicitly as extensible?

I know it's somewhat different, but access modifiers work this way. With the default being restrictive and fuller access only being granted with the insertion of a keyword.

There's a large chance that I haven't thought this through properly, though, so please be humane!

+8  A: 

I see two simple reasons:

  1. Inheritance is a foundational principle of OO, so disallowing it by default wouldn't be intuitive.
  2. The majority of classes are designed to allow inheritance, so allowing inheritance by default saves typing.
Cory House
I think that many would argue that the majority of classes are *not* designed to be inherited from.
Michael Burr
I also argue that most developers do not construct their classes with inheritance in mind.
Steve Horn
Mike and Steve, if you follow good OO practices then you'll be designing classes to be inherited and reused.
Chris Pietschmann
@Chris: No way. Most classes will never be derived from, and guessing which elements require specialisation not only wastes time but builds in restrictions on the course of future implementation decisions. See Effective Java for a good discussion of this.
Jon Skeet
If I could vote up a comment, I would do so for Jon's comment here - that's the fundamental point - designing for inheritance really requires a lot of forethought and the vast majority of the time is unneeded - so why default to something which will require developers to waste brain cycles??
Phil
+3  A: 

I can't recall having heard a rationale for the decision to have classes not sealed by default. However, there are certainly quite a few people who believe that C# should have been spec'ed to have sealed be the default:

http://codebetter.com/blogs/patricksmacchia/archive/2008/01/05/rambling-on-the-sealed-keyword.aspx

Michael Burr
+3  A: 

sealed classes prevent inheritance and therefore are an OO abombination. see this rant for details ;-)

Steven A. Lowe
thanks for the drive-by downvotes with no comment - cowards! :-P
Steven A. Lowe
Personally, I think implementation inheritance is way overstated in importance. And, I'd generally agree with the statement that the default should be sealed - it's extremely easy to screw up with inheritance. Composition should usually be used instead when feasible.So, I strongly disagree with you :)For me the principal advantages of OO development are interfaces and polymorphism.
Phil
@[Phil]: so, every time you create a windows form, do you encapsulate a Form object instead of inherit from System.Windows.Form? Seems like a lot of extra work... Inheritance is fundamental; learn it, live it, love it ;-)
Steven A. Lowe
+3  A: 

You could probably make just as many arguments in favor of sealed-by-default as you could against it. If it were the other way around, someone would be posting the opposite question.

Jon B
+2  A: 

Merely deriving from an unsealed class dosen't change the class's behavior. The worst that can happen is that a new version of the base class will add a member with the same name as the deriving class (in which case there will just be a compiler warning saying you should use the new or override modifier) or the base class is sealed (which is a design no-no if the class has already been released into the wild). Arbitrary sublassing still complies with the Liskov Substitution Principle.

The reason that members are not overridable by default in C# is that because overriding a method can change the base class's behaviour in a way that the base class's author didn't anticipate. By making it explicitly abstract or virtual, it's saying that the author is aware that that it can change or is otherwise beyond their control and the author should have taken this into account.

Mark Cidade
+6  A: 

I'd say it was just a mistake. I know many people (including myself) who believe that classes should indeed be sealed by default. There are at least a couple of people in the C# design team in that camp. The pendulum has swung somewhat away from inheritance since C# was first designed. (It has its place, of course, but I find myself using it relatively rarely.)

For what it's worth, that's not the only mistake along the lines of being too close to Java: personally I'd rather Equals and GetHashCode weren't in object, and that you needed specific Monitor instances for locking too...

Jon Skeet
who on the C# team were pro-sealed-class?
Mark Cidade
Eric Lippert, and Cyrus too I believe. (I don't think either of them was on the team for v1 though.)
Jon Skeet
If GetHashCode weren't in object then every aggregator (not even inheritor) would have to be given access to the privates of every type, and the aggregators would have to roll their own hasher for every type. Do you really really think that would lessen the amount of pain in programming?
Windows programmer
@Windows: Neither of those would be the case. You could *optionally* implement equality and GetHashCode where it made sense, implementing an interface. A system IEqualityComparer could be included which did an identity comparison. Other comparisons could be based on public properties. It'd be fine.
Jon Skeet
+6  A: 

In my opinion there should be no default syntax, that way you always write explicitly what you want. This forces the coder to understand/think more.

If you want a class to be inheritable then you write

public extensible class MyClass

otherwise

public sealed class MyClass

BTW I think the same should go with access modifiers, disallow default access modifiers.

pablito
Good point, you would always have to be clear about what you were doing rather than knowingly or unknowingly falling back on invisible defaults.
frou
you could consider the use of abstract modifier to be used for the purpose of inheritance
Spoike
I would reuse the existing `base` keyword "public base class ..."
Dan
+1  A: 

80% of the features of Word go unused. 80% of classes don't get inherited from. In both cases, once in a while, someone comes along and wants to use or reuse a feature. Why should the original designer prohibit reuse? Let the reuser decide what they want to reuse.

Windows programmer
That's an argument against the existence of the sealed keyword, not the default :)
frou
A: 

For the same reason why objects are not private by default

or

to be consistent with the object analogue, which is objects are not private by default

Just guessing, coz at the end of the day it's a language's design decision and what the creators say is the canon material.

blizpasta