Click here to Skip to main content
15,565,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please tell me answer for this Qsn.
Posted

If you mean "How can I stop classes deriving from my class?" then declare it sealed
If you don't, then please explain further what you do mean!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 21:47pm    
I think not "sealed" but Albin's answer is relevant, because the title mentioned "by creating it's object".
--SA
Sergey Alexandrovich Kryukov 19-Feb-11 22:02pm    
Sorry, no, I've found a number of missing points from Albin's answer, please see mine and my comment.
--SA
Espen Harlinn 20-Feb-11 8:14am    
Reasonable :)
Have a private constructor in that class. So that it can't be instantiated. Also refer singleton patterns
 
Share this answer
 
Comments
Nish Nishant 19-Feb-11 9:43am    
Good answer, voted 5, and proposed as answer.
Albin Abel 19-Feb-11 9:45am    
Thank you
Sergey Alexandrovich Kryukov 19-Feb-11 22:00pm    
This is correct, but this answer is incomplete - by far. Also, singleton topic is, strictly speaking, irrelevant.
Please see my answer to see the points you've missed in your answer.
I decided to vote "4".
--SA
Sergey Alexandrovich Kryukov 20-Feb-11 1:25am    
Sorry, I made a big mistake in my answer, Robert helped to fix it.
--SA
Espen Harlinn 20-Feb-11 8:15am    
Good idea
Albin is right. However, I need to make this answer more accurate.

You should makes sure that all defined constructor's of the class are private (which is the default if there is no access modifier), that is, none of them should be internal or public.

If there are absolutely no constructors, a class still can be instantiated, because an implicit (hidden) parameter-less constructor will be provided (only if there is no other constructors). In this case, if such parameter-less constructor is explicitly provided and private, the class cannot be instantiated; if this is the only purpose for creating such constructor, its body can be empty. (Thanks to Robert Rohde who helped me to fix my mistake in first version of this answer. Sorry for confusion.)

This leaves for the possibility of a back door: such class might have an internal public static function which returns an instance of this class. As such method has access to private constructors, this could be the remaining way to create an instance. You decide what to do with this possibility.

Finally, you can remove the possibility of instantiation by simply declaring the whole class as abstract, even in the case when not members are abstract. The only purpose of such class will be to serve as a base class.

—SA
 
Share this answer
 
v3
Comments
Robert Rohde 20-Feb-11 1:07am    
The default constructor is only created when absolutely no other constructor is defined. You don't need to specifiy it if you have other constructors (with arguments) in place.
Also don't spam all others answers just because you think you've "guessed" what the originalauthor means. The question is vague so its unclear what the poster meant with it.
Sergey Alexandrovich Kryukov 20-Feb-11 1:13am    
Oops. You're right. Your vote of "1" is quite correct because I actually lied, even though by mistake. Will fix.

Sorry, cannot agree with you about "spamming", this is normal communication.
Thank you for your note, this is very helpful.

--SA
Sergey Alexandrovich Kryukov 20-Feb-11 1:24am    
Fixed. Thanks for your help, Robert.
--SA
Olivier Levrey 20-Feb-11 8:15am    
Voted 5. I didn't see the first version of the answer but this one is OK and clearly explains a lot of things.
Sergey Alexandrovich Kryukov 20-Feb-11 12:36pm    
Thank you, Olivier.
I forgot how default works, Robert did not let me lie, which is good :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900