answersLogoWhite

0


Best Answer

I dont think we can have Protected Constructors but yes we can have Private constructors.

We can declare the constructor as Private to ensure that no other class can instantiate it. We use this in the singleton design pattern

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Yes, a constructor can be declared private in a class. However, in order to instantiate the class, you must include a static member function, a friend function or a friend class. The latter is often the preferred method.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes. It is a requirement in singleton classes, where only one instance of the class is expected. The default constructor, copy constructor and the assignment operator are all private. The class itself maintains a static reference to the one and only instance via a static member function usually called GetInstance() or just Instance(). The instance is created upon first accessing the static function; and is guaranteed to exist for the lifetime of the program.

Classes that are entirely static don't require construction so they, too, can be declared private.

Classes that are intended to be base classes must have a protected or public default constructor and destructor. If the class has any virtual methods, the destructor must also be virtual as well.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Yes, yes, and yes. Java constructors may use any access modifier.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

NO.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you define a constructor as private?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you define the constructor as private?

Yes, you can. Making a constructor private ensures that no other class can instantiate that class you just created with a private constructor. It is usually used in Singleton Patterns.


Can you declare default constructor as private?

Yes, but that means you can't define an instance of the class without arguments to new.


When do you get a default constructor When the class has no other constructors When you define at least one constructor When you define a class None of the above?

hjuki


Can you implement a constructor in private part of code?

no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.


What does it mean in java when a constructor is undefined?

When a constructor is not define in java then the instance used in class is not optimised the value and therefore some times it generates some garbage value. By the way , When we not define a constructor then generally it not distrub the execution of the program.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


Is Constructor Can be Private and Protective in c?

question i understand not


When you go for private constructor?

jacks mums fit!


Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


Why do you need a constructor as a class member?

A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.


Can you declare constructor as a private in c plus plus?

Yes. However, like any other private member, a private constructor can only be accessed by the class itself (typically via a static member function) or by one of its friends. There are very few cases where private constructors are appropriate, one of the most common being the need to suppress the compiler-generated copy construction of a base class. However, since C++11, suppressed constructors can simply be deleted, thus making error messages much more meaningful to users of your class. For example, instead of the following: class A { public: A (); // default constructor private: A (const A&); // suppress copy constructor (can still be invoked by the class and its friends) // ... }; You'd now use the following: class A { public: A (); // default constructor A (const A&) =delete; // suppress copy constructor (cannot be invoked at all) // ... };


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors