why are classes sealed?
-
I have always wondered about the logic of making any class sealed :confused: Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
One of the reasons I can think of is that you don't want a programmer to break some important functionality. For example:
sealed class RegulateControlRods
{
if (power > 10MW) LowerRods();
if (power < 5MW) RaiseRods();
}or perhaps change its interface to other classes. For example:
public class RegulateControlRods
{
if (power > 10MW) LowerRods();
if (power < 5MW) RaiseRods();
}public class HomerSimpsonNuclearPowerPlant : RegulateControlRods
{
if (power > 10MW) RunLikeHell();
if (power < 5MW) GoEatADohnut();
}However, sealing things like "Point"??? Sometimes I think both C# and .NET were the result of some highschool student's finals project in Computer Science 101. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka -
Specifically, why are classes like Point and String sealed? This seems really lame. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka MuraokaSecurity I guess. There is a "DerivedClass" app on GotDotNet.com that can make (generated) a wrapped class of string. I also guess it helps in areas where absoluted only that class is required. WebBoxes - Yet another collapsable control, but it relies on a "graphics server" for dynamic pretty rounded corners, cool arrows and unlimited font support.
-
Specifically, why are classes like Point and String sealed? This seems really lame. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraokastd::string is the same, only the compiler does not enforce it, only the absence of virtual destructors indicates that deriving from it is not what the implimenters want. Use has-a rather than is-a to extend these classes. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002 -
One of the reasons I can think of is that you don't want a programmer to break some important functionality. For example:
sealed class RegulateControlRods
{
if (power > 10MW) LowerRods();
if (power < 5MW) RaiseRods();
}or perhaps change its interface to other classes. For example:
public class RegulateControlRods
{
if (power > 10MW) LowerRods();
if (power < 5MW) RaiseRods();
}public class HomerSimpsonNuclearPowerPlant : RegulateControlRods
{
if (power > 10MW) RunLikeHell();
if (power < 5MW) GoEatADohnut();
}However, sealing things like "Point"??? Sometimes I think both C# and .NET were the result of some highschool student's finals project in Computer Science 101. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka MuraokaMarc Clifton wrote: Sometimes I think both C# and .NET were the result of some highschool student's finals project in Computer Science 101. If someone did this for a final semester project I guess he's still in college ;-) Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
std::string is the same, only the compiler does not enforce it, only the absence of virtual destructors indicates that deriving from it is not what the implimenters want. Use has-a rather than is-a to extend these classes. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002Christian Graus wrote: Use has-a rather than is-a to extend these classes. What a waste of time just cause they made it sealed. Like you said it doesn't stop anhyone from has-a extending the class. Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
Christian Graus wrote: Use has-a rather than is-a to extend these classes. What a waste of time just cause they made it sealed. Like you said it doesn't stop anhyone from has-a extending the class. Nish
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
It's not a waste of time, it's good design. The resultant class will not be derived from String, it cannot take the place of a string. This is by design. I like it. You should read 'Design and Evolution of C++' to get some insight into this sort of stuff. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002 -
It's not a waste of time, it's good design. The resultant class will not be derived from String, it cannot take the place of a string. This is by design. I like it. You should read 'Design and Evolution of C++' to get some insight into this sort of stuff. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002I'll take a look at the book, but it does seem very inconvenient, because if I wanted it to act like an "is-a" class I'd have to write pass-throughs for all the base class's methods. For something like String, this is a lot! Incidentally, this is also why I'm not very enamoured with interfaces. The derived class ends up having to write the implementation. Maybe I'm missing some core paradigm shift in OO programming. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka -
I'll take a look at the book, but it does seem very inconvenient, because if I wanted it to act like an "is-a" class I'd have to write pass-throughs for all the base class's methods. For something like String, this is a lot! Incidentally, this is also why I'm not very enamoured with interfaces. The derived class ends up having to write the implementation. Maybe I'm missing some core paradigm shift in OO programming. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka MuraokaMarc Clifton wrote: because if I wanted it to act like an "is-a" class I'd have to write pass-throughs for all the base class's methods. For something like String, this is a lot! The point is that this is by design, it's a feature for the benefit of class implimenters. I was pretty annoyed that I could not derive from std::string also, but once I knew the reasons I was fine with it. Incidentally, C++ would let me derive from std::string, all they could do was write it so that anyone who knew what they were doing would know it was a bad idea. C# is etter in that it allows you to generate a compile error if you derive from a class which the implimenter did not want you to. Marc Clifton wrote: Incidentally, this is also why I'm not very enamoured with interfaces. I don't like interfaces/no multiple inheritance either. Marc Clifton wrote: Maybe I'm missing some core paradigm shift in OO programming. There's no paradigm shift that I can see, it's simply the ability to enforce something that you could only strongly suggest in C++. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002 -
Specifically, why are classes like Point and String sealed? This seems really lame. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka MuraokaPoint is sealed because it is a struct, and structs don't allow inheritence. String is sealed because if some things were to change with its behavior it would cause vast shockwaves to flow throughout the framework because the way it is used is based on its implementation. If you were to modify String so that strings could mutate then vast portions of the framework wouldn't work correctly because the framework assumes string can't mutate. As someone else pointed out, allowing some classes to be derived from could cause problems security wise. James [edit]fixed typo[/edit] [edit2]yeesh, I think I need some sleep, clarified point about string[/edit2] "The elastic retreat rings the close of play as the last wave uncovers the newfangled way. But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick." "Thick as a Brick" from Thick as a Brick, Jethro Tull 1972
-
Point is sealed because it is a struct, and structs don't allow inheritence. String is sealed because if some things were to change with its behavior it would cause vast shockwaves to flow throughout the framework because the way it is used is based on its implementation. If you were to modify String so that strings could mutate then vast portions of the framework wouldn't work correctly because the framework assumes string can't mutate. As someone else pointed out, allowing some classes to be derived from could cause problems security wise. James [edit]fixed typo[/edit] [edit2]yeesh, I think I need some sleep, clarified point about string[/edit2] "The elastic retreat rings the close of play as the last wave uncovers the newfangled way. But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick." "Thick as a Brick" from Thick as a Brick, Jethro Tull 1972
String may be sealed simply because it is a "special" object. The .NET runtime does some things with strings internally that you can't do by using a String object, mostly in the name of efficiency. For instance, a "mutable" String is used inside the StringBuilder class, producing a much more efficient implementation. Change the implementation of String, and you don't just get errant code - you might get GPFs, memory leaks, or corrupted memory.
-
String may be sealed simply because it is a "special" object. The .NET runtime does some things with strings internally that you can't do by using a String object, mostly in the name of efficiency. For instance, a "mutable" String is used inside the StringBuilder class, producing a much more efficient implementation. Change the implementation of String, and you don't just get errant code - you might get GPFs, memory leaks, or corrupted memory.
JasonSmith wrote: Change the implementation of String, and you don't just get errant code - you might get GPFs, memory leaks, or corrupted memory. Even worse, if you mutate a string using unsafe code the program works great only the output is a bit funky. Someone posted some code doing this in the Soapbox quite a while ago, but essentially because of string interning you can change the textual representation of some things like True and False, that are output by boolean.ToString() ;P String interning is another optimization that wouldn't be possible if strings were allowed to mutate. If two strings are interned, then you can compare them by simply comparing the references or their location in the internal cache of strings. If they aren't interned, or you need case-insensitive comparisons then you need compare each character in both strings. James "The elastic retreat rings the close of play as the last wave uncovers the newfangled way. But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick." "Thick as a Brick" from Thick as a Brick, Jethro Tull 1972
-
Specifically, why are classes like Point and String sealed? This seems really lame. Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka MuraokaMarc Clifton wrote: Specifically, why are classes like Point and String sealed? Jason mentioned something that got me to remember another reason for sealing classes. JIT optimizations when dealing with virtual methods ;-) If you have a virtual method in a class and you seal one of its child classes the JIT can produce code to call the child class' implementation instead of going to a vtable approach that would otherwise be needed. It still needs to rely on the vtable if the variable is of the parent class type, but when you specify the child class it isn't needed. Sorry of that last paragraph didn't make sense, I couldn't think of a better way of describing it :( James "The elastic retreat rings the close of play as the last wave uncovers the newfangled way. But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick." "Thick as a Brick" from Thick as a Brick, Jethro Tull 1972
-
Marc Clifton wrote: Specifically, why are classes like Point and String sealed? Jason mentioned something that got me to remember another reason for sealing classes. JIT optimizations when dealing with virtual methods ;-) If you have a virtual method in a class and you seal one of its child classes the JIT can produce code to call the child class' implementation instead of going to a vtable approach that would otherwise be needed. It still needs to rely on the vtable if the variable is of the parent class type, but when you specify the child class it isn't needed. Sorry of that last paragraph didn't make sense, I couldn't think of a better way of describing it :( James "The elastic retreat rings the close of play as the last wave uncovers the newfangled way. But your new shoes are worn at the heels and your suntan does rapidly peel and your wise men don't know how it feels to be thick as a brick." "Thick as a Brick" from Thick as a Brick, Jethro Tull 1972
It made sense. I read about that somewhere--thanks for reminding me! Marc Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka