Friday, July 27, 2012

Difference between Private and Sealed Classes?


Sealed Class:


"Sealed" keyword provide us facility to prevent a class to be inherited by other classes, so "Sealed" classes cannot be inherited by any other class.

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

"Sealed" class can have constructor, and they can only be used by its object (non-static methods).

Private Class:


Private class cannot be used directly under any namespace, means we can only create private class as a nested class, to make it available only to class to whom its nested.

the only/mostly use of Private class/structure is to create a user-defined type, which you wants to be accessible to that class only. 

NOTE:Nested/inner class can be public also and that can be accessible to inherited class or by object where as private nested/inner class is only accessible to its outer class .

1 comment:

Varshith said...

Is that really cool?