473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

application of sealed keyword


Would somebody know when we should seal a class? Shouldn't all classes be
open up for inheritance?

Thanks!
Nov 15 '05 #1
14 2928
Zeng wrote:
Would somebody know when we should seal a class? Shouldn't all
classes be open up for inheritance?


That's the whole point: that decision is left to the designer.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
There are some things that you don't want inheritable. The main thing that
comes to mind is that i fyou don't have to worry about anyone inheriting
from your class, you can change virtual function invokes to non-virtual
invokes. May not sound like a big deal, but it can be. Granted, there
aren't a whole lot of instances for this, but there are enough for it to be
noteworthy. Another instance is when you may have all static members

http://msdn.microsoft.com/library/de...tml/copybr.asp
"Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...

Would somebody know when we should seal a class? Shouldn't all classes be
open up for inheritance?

Thanks!

Nov 15 '05 #3
Thanks for replying, I can see the first instance you pointed out, but the
second one..hm, if the guy next door wants to inherit my class which has all
staic members, then it would be up to him, there isn't a design issue there.

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
There are some things that you don't want inheritable. The main thing that comes to mind is that i fyou don't have to worry about anyone inheriting
from your class, you can change virtual function invokes to non-virtual
invokes. May not sound like a big deal, but it can be. Granted, there
aren't a whole lot of instances for this, but there are enough for it to be noteworthy. Another instance is when you may have all static members

http://msdn.microsoft.com/library/de...tml/copybr.asp "Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...

Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance?

Thanks!


Nov 15 '05 #4
If the class by design had static members, mixing them could cause problems.
Think about Brushes for instance....
"Zeng" <zz*@nonospam.c om> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for replying, I can see the first instance you pointed out, but the
second one..hm, if the guy next door wants to inherit my class which has all staic members, then it would be up to him, there isn't a design issue there.
"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
There are some things that you don't want inheritable. The main thing

that
comes to mind is that i fyou don't have to worry about anyone inheriting
from your class, you can change virtual function invokes to non-virtual
invokes. May not sound like a big deal, but it can be. Granted, there
aren't a whole lot of instances for this, but there are enough for it to

be
noteworthy. Another instance is when you may have all static members

http://msdn.microsoft.com/library/de...tml/copybr.asp
"Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...

Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance?

Thanks!



Nov 15 '05 #5
Would you please be more specific? I'm not quite following your example.
Thanks!
"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
If the class by design had static members, mixing them could cause problems. Think about Brushes for instance....
"Zeng" <zz*@nonospam.c om> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for replying, I can see the first instance you pointed out, but the second one..hm, if the guy next door wants to inherit my class which has

all
staic members, then it would be up to him, there isn't a design issue

there.

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
There are some things that you don't want inheritable. The main thing

that
comes to mind is that i fyou don't have to worry about anyone inheriting from your class, you can change virtual function invokes to non-virtual invokes. May not sound like a big deal, but it can be. Granted, there aren't a whole lot of instances for this, but there are enough for it to
be
noteworthy. Another instance is when you may have all static members

http://msdn.microsoft.com/library/de...tml/copybr.asp "Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
>
> Would somebody know when we should seal a class? Shouldn't all
classes be
> open up for inheritance?
>
> Thanks!
>
>



Nov 15 '05 #6
By the way, about the first design usage, you could just use the scope of
your class if you want to avoid calling virtual function; in addition, with
a good design, the override method of a derived class should always replace
the functionality of the one it overrides

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
If the class by design had static members, mixing them could cause problems. Think about Brushes for instance....
"Zeng" <zz*@nonospam.c om> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for replying, I can see the first instance you pointed out, but the second one..hm, if the guy next door wants to inherit my class which has

all
staic members, then it would be up to him, there isn't a design issue

there.

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
There are some things that you don't want inheritable. The main thing

that
comes to mind is that i fyou don't have to worry about anyone inheriting from your class, you can change virtual function invokes to non-virtual invokes. May not sound like a big deal, but it can be. Granted, there aren't a whole lot of instances for this, but there are enough for it to
be
noteworthy. Another instance is when you may have all static members

http://msdn.microsoft.com/library/de...tml/copybr.asp "Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...
>
> Would somebody know when we should seal a class? Shouldn't all
classes be
> open up for inheritance?
>
> Thanks!
>
>



Nov 15 '05 #7
Zeng:

You're right, you could. However, that's a lot of work when you could just
seal it. There's almost always a few ways to get somewhere, but some
routes are more direct.
"Zeng" <zz*@nonospam.c om> wrote in message
news:%2******** **********@tk2m sftngp13.phx.gb l...
By the way, about the first design usage, you could just use the scope of
your class if you want to avoid calling virtual function; in addition, with a good design, the override method of a derived class should always replace the functionality of the one it overrides

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
If the class by design had static members, mixing them could cause problems.
Think about Brushes for instance....
"Zeng" <zz*@nonospam.c om> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for replying, I can see the first instance you pointed out, but the second one..hm, if the guy next door wants to inherit my class which has
all
staic members, then it would be up to him, there isn't a design issue

there.

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:Oq******** ******@TK2MSFTN GP09.phx.gbl...
> There are some things that you don't want inheritable. The main
thing that
> comes to mind is that i fyou don't have to worry about anyone

inheriting > from your class, you can change virtual function invokes to non-virtual > invokes. May not sound like a big deal, but it can be. Granted, there > aren't a whole lot of instances for this, but there are enough for it to
be
> noteworthy. Another instance is when you may have all static
members >
>

http://msdn.microsoft.com/library/de...tml/copybr.asp
> "Zeng" <zz*@nonospam.c om> wrote in message
> news:OO******** ******@TK2MSFTN GP12.phx.gbl...
> >
> > Would somebody know when we should seal a class? Shouldn't all classes be
> > open up for inheritance?
> >
> > Thanks!
> >
> >
>
>



Nov 15 '05 #8
Hello

First: If the sealed class is inheriting another class, using the sealed
keyword either in the method or class declaration make the CLR convert all
calls from virtual to normal method calls, because the runtime knows that
this method and can't be overriden. This can improve performance.

Second. When the class designer knows that the overriding of his method can
affect the functionality of his class. For example a method that performs a
security check before making a call to another method. The inheritor of the
method can override the method and calling the other method directly without
the security check, so you may want to prevent anyone from doing that.

Third, if the class has only static fields and properties, it doesn't make
sense to inherit the class. So the class designer can choose to seal his
class to avoid confusion.

Best regards

Sherif

"Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...

Would somebody know when we should seal a class? Shouldn't all classes be
open up for inheritance?

Thanks!

Nov 15 '05 #9
Thanks for responding, among the three, I could see the first one being a
good reason for sealed keyword.

For the second one, if security is checked inside a loose method like that,
I can just wrap the class (~ private inheritance in C++ ) and call directly
whatever method I want w/o going through the security checking method. It
sounds to me that a bigger design issue exists for the example you described

For the third, if someone wants to inherit my static-method-only class then
be it, no programmer should or would inherit a class that has nothing to be
inherited, and because there isn't a un-planned consequence, we shouldn't
have to force a policy on it.

Thanks!

"Sherif ElMetainy" <el************ *@wayout.net.NO SPAM> wrote in message
news:OI******** ******@tk2msftn gp13.phx.gbl...
Hello

First: If the sealed class is inheriting another class, using the sealed
keyword either in the method or class declaration make the CLR convert all
calls from virtual to normal method calls, because the runtime knows that
this method and can't be overriden. This can improve performance.

Second. When the class designer knows that the overriding of his method can affect the functionality of his class. For example a method that performs a security check before making a call to another method. The inheritor of the method can override the method and calling the other method directly without the security check, so you may want to prevent anyone from doing that.

Third, if the class has only static fields and properties, it doesn't make
sense to inherit the class. So the class designer can choose to seal his
class to avoid confusion.

Best regards

Sherif

"Zeng" <zz*@nonospam.c om> wrote in message
news:OO******** ******@TK2MSFTN GP12.phx.gbl...

Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance?

Thanks!


Nov 15 '05 #10

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

Similar topics

6
2249
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 efficiency reasons (optimizing away virtual function calls, etc.) that justify it in other languages,...
5
7631
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
4
12872
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for C# 3.0? Any comments? Néstor
3
6470
by: avnrao | last post by:
Hi, I am curious about how to write C# class with this requirement : I want to make one of the methods of the class to be non overridable in derived classes. I cannot use sealed because sealed keyword on a method should also have override keyword. My class here is the master class. Consider a class Master with 2 methods
9
8403
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
9
3975
by: lee.chappers | last post by:
How can I make the CodeDom generate the following C# method? protected sealed override void Test() { } I've tried using: domMethod.Attributes = MemberAttributes.Override | MemberAttributes.Family | MemberAttributes.Final;
18
2946
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
2341
by: Hilton | last post by:
Jon Skeet wrote: I for one wish the "sealed" keyword on a class didn't exist. Why can I not have: class NamedThread : Thread { string name; :
1
1840
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
8352
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.