Hai friends what is the difference between sealed class and private constructor any way both doesnt allow inheritance? If any difference please tell me?

Hai friends what is the difference between sealed class and private constructor any way both doesnt allow inheritance? If any difference please tell me?

Let me takes a shot at this ...

A sealed class doesn't allow inheritance. That is, you can't derive from it.

A private constructor doesn't allow instancing (you can instance a sealed class). Also when you get deeper into the language, private constructors can be used in scenarios where you're overloading constructors.

So while it is true that you can't inherit either, its really a result of the design approach, not the reasoning behind them.

Any of the God Guru's here are free to correct me if I'm wrong (that way I learn more too).

Sealed classes
A sealed class cannot be used as a base class. For this reason, it also cannot be an abstract class. Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency.

Sealed Class:
When applied to a class, the sealed modifier prevents other
classes from inheriting from it. In the following example,
class B inherits from class A, but no class can inherit
from class B.

class A {}
sealed class B : A {}

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.

Private Class :

Classes and structs that are not nested within other
classes or structs can be either public or internal. A type
declared as public is accessible by any other type. A type
declared as internal is only accessible by types within the
same assembly. Classes and structs are declared as internal
by default unless the keyword public is added to the class
definition. And yes, a class cannot be private unless of
course if it a nested class.


In private Class: We can create a constructor and therefore we can create an instance of that class.
But In Sealed class we can not create a constructor of that class, so no instance of that class is possible.
Private Constructor of a Private Class = Sealed Class.

But In Sealed class we can not create a constructor of that class, so no instance of that class is possible.
Private Constructor of a Private Class = Sealed Class.

Sorry but that is wrong. Let's take this for example...

public sealed class MySingleton
{
   private class Nested
   {
      static Nested(){}
      internal static readonly MySingleton MySingleInstance = new MySingleton(); // I just instantiated a sealed class...    
   }
   
   public static Instance { get { return Nested.MySingleInstance; } }

   public MySingleton()
   {
      // This is my constructor and is perfectly valid
   }
   
   /* I can make all my instance methods here */
}

So you can create a constructor of a sealed class. Sealed allows you to prevent inheritance but still created instances of a class.

A private constructor prevents both inheritance and instantiation. You should use a private constructor if you wish to use a method to create instances of other classes (like a management object) or if it doesn't contain any non-static fields or methods.

thanks friends.. finally what i get is i can create an instance for sealed class but not the class where i use private constructor...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.