.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Interview Question >> OOPS >> Post New Question Subscribe to Interview Questions

What is a private constructor? Where will you use it?

Posted By :Virendra Dugar     Posted Date :October 07, 2009    Points :10   Category :OOPS 
When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.

You can also find related Interview Question to What is a private constructor? Where will you use it?  below: 

What is Private Constructor? and it's use? Can you create instance of a class which has Private Constructor?

  
When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
Make a constructor private if:

1) You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class' Clone method.

2) You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory. (More...)

A constructor can be private. True or False

  
False. A constructor should not be private. (More...)

Whether we can create a private constructor for a class?

  
Yes it is possible. we can create a private constructor for a class (More...)

Where we can use the private constructor?

  
A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. (More...)

Which design pattern should have private or protected constructor?

  
singleton design pattern should have private or protected constructor (More...)

What is the use of private constructor in Java?

  
A private constructor is used when there is no requirement of another class to invoke that. It is used mostly in implementing singletons. A static method is used for managing the creating the instances of that class.

The following is the example of private constructor.

public class Singleton {
private static Singleton instance;.
//private constructor
private Singleton {
// some code goes here
}
} (More...)

Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

  
(Class constructor method is also known as type constructor or type initializer)
Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. (It will get executed only 1st time, when we call any static methods/fields in the same class.) Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention. (More...)

What are private assemblies and shared assemblies?

  
A private assembly is used only by a single application, and is stored in that application's install directory (or a subdirectory therein). A shared assembly is one that can be referenced by more than one application. In order to share an assembly, the assembly must be explicitly built for this purpose by giving it a cryptographically strong name (referred to as a shared name). By contrast, a private assembly name need only be unique within the application that uses it.
By making a distinction between private and shared assemblies, we introduce the notion of sharing as an explicit decision. Simply by deploying private assemblies to an application directory, you can guarantee that that application will run only with the bits it was built and deployed with. References to private assemblies will only be resolved locally to the private application directory.
There are several reasons you may elect to build and use shared assemblies, such as the ability to express version policy. The fact that shared assemblies have a cryptographically strong name means that only the author of the assembly has the key to produce a new version of that assembly. Thus, if you make a policy statement that says you want to accept a new version of an assembly, you can have some confidence that version updates will be controlled and verified by the author. Otherwise, you don't have to accept them.
For locally installed applications, a shared assembly is typically explicitly installed into the global assembly cache (a local cache of assemblies maintained by the .NET Framework). Key to the version management features of the .NET Framework is that downloaded code does not affect the execution of locally installed applications. Downloaded code is put in a special download cache and is not globally available on the machine even if some of the downloaded components are built as shared assemblies.
The classes that ship with the .NET Framework are all built as shared assemblies. (More...)

Constructor and Destructor

  
Constructor

1. The Constructor is the first method that is run when an instance of a type is created. In visual basic a constructor is always Sub new ().

2. Constructor are use to initialize class and structure data before use. Constructor never returns a value and can be overridden to provide custom initialization functionality.

3. The constructor provides a way to set default values for data or perform other necessary functions before the object available for use.

Destructor:
-------------
Destructors are called just before an object is destroyed and can be used to run clean-up code. You can't control when a destructor is called. (More...)

Is versioning applicable to private assemblies?

  
Versioning concept is only applicable to global assembly cache (GAC) as private assembly lie in their individual folders. (More...)

What is the Scope of public/private/friend/protected/protected friend?

  
Scope of public/private/friend/protected/protected friend.

Visual Basic/Visual C#
Public/public All members in all classes and projects.

Private/private Members of the current class only.

Friend/internal All members in the current project.

Protected/protected All members in the current class and in classes derived from this member's class. Can be used only in member definitions, not for class or module definitions.

Protected Friend/protected internal All members in the current project and all members in classes derived from this member's class. Can be used only in member definitions, not for class
or module definitions. (More...)

Does CLR impose a strict versioning policy for the private assembly?

  
Answer is No. Private assembly are used by the application in which they are deployed. To use private assembly in any application, one needs to deploy them in the application's directory. Versioning needs to done for shared assembly which are stored in GAC and used by multiple applications. (More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend