473,569 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why use the 'sealed' ?

any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga******* @gmail.com
ICQ:156134382
Nov 17 '05 #1
9 8399
Kylin <ga*******@gmai l.com> wrote:
any better reason ?


Any better reason than what?

Use sealed when either you want to override a property/method but make
sure that it can't be further overridden, or when you want to make sure
that a class can't be derived from.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Suppose that you have something like this;

public abstract class anAbstractClass
{
public abstract void someMethod();
public abstract void anotherMethod() ;
}

public class FirstDerivative Class : anAbstractClass
{
public sealed override void someMethod()
{
}
public override void anotherMethod()
{
}
}

public class SecondDerivativ eClass : FirstDerivative Class
{
public override void anotherMethod()
{
}
}
public sealed class ThirdDerivative Class : SecondDerivativ eClass
{
public override void anotherMethod()
{
}
}
Now you have an abstract class and two abstract methods. At
FirstDerivative Class, you implement someMethod() and do not want to anybody
to override it. You define it as sealed method. At SecondDerivativ eClass you
can not override someMethod() but can override anotherMethod. At
ThirdDerivative Class you still can override anotherMethod. But you guarantee
that there can not be a fourth derivative class by defining that class as a
sealed. Is it clear?
--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"Kylin" <ga*******@gmai l.com> wrote in message
news:Ob******** ******@TK2MSFTN GP12.phx.gbl...
any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga******* @gmail.com
ICQ:156134382

Nov 17 '05 #3
As a general rule, I avoid sealing my classes. It seems that invariably
I'll want to add functionality to something at a later date. I'm not
even sure that I like the idea that methods and classes can be
nonvirtual. And I really don't like the fact that static members are
always final.

While I'm about to do some complaining about certain .NET things, I'm
going to introduce it by saying that I believe .NET is a great product
for something still in its first version. The guys at Microsoft did a
great job.

I sortof wish you could add functionality to (or replace members of)
existing classes in C# like you can in languages like Ruby. -- like
being able to add the Left/Right methods to the string class for the
scope of an application, or replacing the Substring function with
something that doesn't throw an exception when the Length parameter
exceeds past the end of the string.

I kicked the air a few times when I noticed how many members of the
ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
were not sealed, and say, the "Fill" method was marked as virtual, you
could easily write some great diagnostic tools.

For instance, say you wanted the ability to view all of the raw
datasets going to an application. All that would be necessary would be
to subclass the SqlDataAdapter class, override the Fill method with
something that would, say, raise an event with your data. Then write a
simple windows application that spawned a window with a datagrid
populated with that data every time Fill was called. Have that windows
application act as a Remoting Server for your application, and set the
application to be a Remoting Client. When you're done diagnosing,
simply remove your remoting configuration.

-Alan

Kylin wrote:
any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga******* @gmail.com
ICQ:156134382


Nov 17 '05 #4
Alan Samet <al*******@gmai l.com> wrote:
As a general rule, I avoid sealing my classes. It seems that invariably
I'll want to add functionality to something at a later date. I'm not
even sure that I like the idea that methods and classes can be
nonvirtual.
Personally, I love it. Designing classes to be derived from properly is
a lot of work, and shouldn't be undertaken when it's not necessary. To
be honest, I wish classes were sealed by default.
And I really don't like the fact that static members are
always final.
Well, how would you expect polymorphism to work with them, seeing as
you can't invoke a static method on an instance anyway? Which static
method is invoked is a compile-time decision, so it doesn't make sense
for them to be virtual.
While I'm about to do some complaining about certain .NET things, I'm
going to introduce it by saying that I believe .NET is a great product
for something still in its first version. The guys at Microsoft did a
great job.

I sortof wish you could add functionality to (or replace members of)
existing classes in C# like you can in languages like Ruby. -- like
being able to add the Left/Right methods to the string class for the
scope of an application, or replacing the Substring function with
something that doesn't throw an exception when the Length parameter
exceeds past the end of the string.
Sounds dangerous to me - sounds like the kind of thing which could
easily introduce unwanted and possibly insecure behaviour. For
instance, I know that when I use a string I can call any method and it
won't change the contents of the string. If one piece of code could
change the implementation of string, all the rest of my code would have
to take that into account.
I kicked the air a few times when I noticed how many members of the
ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
were not sealed, and say, the "Fill" method was marked as virtual, you
could easily write some great diagnostic tools.
But then the situations in which Fill is called for you from other
methods would have to be documented, and would then be set in stone,
etc. Like I said, designing for derivation is time-consuming.
For instance, say you wanted the ability to view all of the raw
datasets going to an application. All that would be necessary would be
to subclass the SqlDataAdapter class, override the Fill method with
something that would, say, raise an event with your data. Then write a
simple windows application that spawned a window with a datagrid
populated with that data every time Fill was called. Have that windows
application act as a Remoting Server for your application, and set the
application to be a Remoting Client. When you're done diagnosing,
simply remove your remoting configuration.


I can see how it's useful in certain situations, but I think the
downsides outweigh the upsides in general. Of course, with a proper AOP
system in place, you could have the above without making Fill itself
virtual.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
>>I wish classes were sealed by default

That goes completely against the grain of oop concepts. The whole idea of a
system that supports inheritance and polymorphism is to promote reusability.
Sealing a class is an architectural descision taken when a provider wants to
prohibit that process even when a potential consumer of the code sees a need
to derive from it. It's not one that should be taken lightly or left as a
default case because that would mean that many classes would leave the
factory floor with unintentional sealing in place. The sealed keyword must
be explicit because it's connotations are so far reacing.

Like Alan I think that the sealed state of many of the .NET classes are an
architectural nightmare that prohibits people from using classes that are so
deficient that they need overriding just to make sense of them.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Alan Samet <al*******@gmai l.com> wrote:
As a general rule, I avoid sealing my classes. It seems that invariably
I'll want to add functionality to something at a later date. I'm not
even sure that I like the idea that methods and classes can be
nonvirtual.


Personally, I love it. Designing classes to be derived from properly is
a lot of work, and shouldn't be undertaken when it's not necessary. To
be honest, I wish classes were sealed by default.
And I really don't like the fact that static members are
always final.


Well, how would you expect polymorphism to work with them, seeing as
you can't invoke a static method on an instance anyway? Which static
method is invoked is a compile-time decision, so it doesn't make sense
for them to be virtual.
While I'm about to do some complaining about certain .NET things, I'm
going to introduce it by saying that I believe .NET is a great product
for something still in its first version. The guys at Microsoft did a
great job.

I sortof wish you could add functionality to (or replace members of)
existing classes in C# like you can in languages like Ruby. -- like
being able to add the Left/Right methods to the string class for the
scope of an application, or replacing the Substring function with
something that doesn't throw an exception when the Length parameter
exceeds past the end of the string.


Sounds dangerous to me - sounds like the kind of thing which could
easily introduce unwanted and possibly insecure behaviour. For
instance, I know that when I use a string I can call any method and it
won't change the contents of the string. If one piece of code could
change the implementation of string, all the rest of my code would have
to take that into account.
I kicked the air a few times when I noticed how many members of the
ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
were not sealed, and say, the "Fill" method was marked as virtual, you
could easily write some great diagnostic tools.


But then the situations in which Fill is called for you from other
methods would have to be documented, and would then be set in stone,
etc. Like I said, designing for derivation is time-consuming.
For instance, say you wanted the ability to view all of the raw
datasets going to an application. All that would be necessary would be
to subclass the SqlDataAdapter class, override the Fill method with
something that would, say, raise an event with your data. Then write a
simple windows application that spawned a window with a datagrid
populated with that data every time Fill was called. Have that windows
application act as a Remoting Server for your application, and set the
application to be a Remoting Client. When you're done diagnosing,
simply remove your remoting configuration.


I can see how it's useful in certain situations, but I think the
downsides outweigh the upsides in general. Of course, with a proper AOP
system in place, you could have the above without making Fill itself
virtual.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
-- snip --
[Alan Samet] For instance, say you wanted the ability to view all of the raw datasets going to an application. All that would be necessary would be to subclass the SqlDataAdapter class, override the Fill method with
something that would, say, raise an event with your data. Then write a simple windows application that spawned a window with a datagrid
populated with that data every time Fill was called. Have that windows application act as a Remoting Server for your application, and set the application to be a Remoting Client. When you're done diagnosing,
simply remove your remoting configuration.


Jon Skeet [ C# MVP ] wrote: I can see how it's useful in certain situations, but I think the
downsides outweigh the upsides in general. Of course, with a proper AOP system in place, you could have the above without making Fill itself
virtual.


I couldn't agree with you more on this.

On a side note, it looks as if Boo will provide a lot of the stuff that
I'd like to see in a .NET language:

http://en.wikipedia.org/wiki/Boo_programming_language

-Alan

Nov 17 '05 #7
Hi Bob.... Here is some info on what Jon is talking about.
http://portal.acm.org/citation.cfm?id=28702
Regards,
Jeff
That goes completely against the grain of oop concepts.<


*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8
"Kylin" <ga*******@gmai l.com> wrote in message
news:Ob******** ******@TK2MSFTN GP12.phx.gbl...
any better reason ?


If a method (or a set of methods) provides a contract (explicit or de facto)
to a consumer that it will behave in a certain way, with certain
side-effects, and these results, then with inheritance the original method
loses the ability to make that guarantee when something else comes along and
inserts itself in its place. At this point all bets are off and the
original guarantee to the consumer is no longer valid. If the loss of this
guarantee would be of too great a consequence to the consumer then the class
designer can seal the class to prevent this situation.

-- Alan
Nov 17 '05 #9
Bob Powell [MVP] <bob@_spamkille r_bobpowell.net > wrote:
I wish classes were sealed by default

That goes completely against the grain of oop concepts.


Why? It wouldn't change what was possible at all - just the default.
The whole idea of a system that supports inheritance and polymorphism
is to promote reusability.
Well, that's certainly *part* of the idea, IMO. There's more to it than
just that, and I don't see why making things sealed by default would
cause problems there.

I very rarely derive from another of my classes which I haven't
explicitly considered suitable for derivation to start with. It creates
all kinds of headaches.
Sealing a class is an architectural descision taken when a provider wants to
prohibit that process even when a potential consumer of the code sees a need
to derive from it. It's not one that should be taken lightly or left as a
default case because that would mean that many classes would leave the
factory floor with unintentional sealing in place. The sealed keyword must
be explicit because it's connotations are so far reacing.
Whereas I see leaving things unsealed as having far reaching
implications.
Like Alan I think that the sealed state of many of the .NET classes are an
architectural nightmare that prohibits people from using classes that are so
deficient that they need overriding just to make sense of them.


Those are problems with the classes' design in other ways than being
sealed though. Deriving from a class to fix design flaws is hardly
elegant.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2246
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are techniques to accomplish this. From what I've heard, sealed may be added to the language in the future?) I understand that there are compiler and...
5
7627
by: Bharat Karia | last post by:
Hi, Is it possible to writed Sealed classes in C++ . i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error and the compiler should flag it as such. Thanks Bharat Karia
14
2919
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
3
4903
by: cmrchs | last post by:
Hi, why is the String class sealed ? wouldn't it be nice to have a class MyString : String { ... } containing already all the nice features of String ?
13
8091
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that the code inside the constructor / desconstructor will never run? Mark
10
3853
by: TJM | last post by:
Hi, Is it possible to have a method sealed and abstract at the same time? MSDN states clearly that this is not allowed for classes but it does not mention it for methods. I tried with a simple example and the compiler would not allow me to compile, however in a recent interview, I was asked this question and the interviewer claimed that...
18
2941
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent definition is perfectly legal in other CLR languages. For example in C#; static class XXX
7
1834
by: Zytan | last post by:
I know you cannot have a sealed static class, but why not? Why must static classes be left open to inheritance? This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/ recommends to place DLL imports into a sealed class, with a private constructor to prevent instantiation (see Figure 1 at the very top):...
1
1839
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks, Mulugeta
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.