Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I have to seal a class without using 'Seal keyword', how would I achieve that?

Don't say that we can seal the methods of that class.
Posted
Updated 14-Dec-10 22:19pm
v2
Comments
Dalek Dave 15-Dec-10 4:19am    
Edited for Grammar and Readability.

Hi
To make a class "Sealed" means it can not be served as a "Base class" to any other class(es).

Without "sealed" keyword it can be achived following way,

1. Write an explicit constructor of the class, overwriting default system provided constructor.
2. Make the constructor as "private".

namespace N
{
    class A
    {
        private A()
        {

        }
    }
}

Above code is "equivalent" to a "sealed class", here class A have been made as sealed class, restricting other class(es) to derive from "A" irrespective of whatever the namespace is.

Let me know if that helps,
 
Share this answer
 
Comments
Dalek Dave 15-Dec-10 4:19am    
Good Call.
Arindam Tewary 15-Dec-10 4:21am    
Thanks Dave :)
fjdiewornncalwe 25-Dec-10 18:29pm    
Nicely done...
Since I suppose this was an interview question - "One way to do this would be to make a private class and then use it as a nested class. No one can inherit that class then."

Of course, there must be another better alternate solution which I am totally unaware of.
:)
 
Share this answer
 
v2
Comments
shikhar gilhotra 15-Dec-10 3:23am    
plz give example abhi,,

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



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