answersLogoWhite

0


Best Answer

Constructors are not declared public only. They can be declared protected and private, as required by the class. Protected constructors are only accessible to the class members and to derived classes. Private constructors are only accessible to the class members.

Although a default public constructor is required by the majority of classes, it is not true of all classes. Derived classes can call any public or protected base class constructor explicitly via their own construction initialisation sections.

Private construction is typically used in the singleton model to instantiate a private static instance of the class as and when it is required, whilst preventing multiple instances of the class from being created. However, class members also include friends of the class, thus external friend classes and friend functions can also instantiate objects via their private constructors.

Note that the whole point of public, protected and private access is not to hide information (as is often wrongly said) but in order to limit access to that information. The same applies to a class' constructors as it does to its member methods and member variables.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why constructors are declared public only?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

Which country only declared Christmas Day a public holiday in 1958?

Scotland


How can you create a virtual copy constructor?

You cannot. Constructors are specific to the class in which they are declared. They cannot be inherited and so they cannot be virtual.


What is a sample of multiple constructor in a single class?

public class Test { public Test() { … } public Test(int i) { … } public Test(String s) { … } } In the above example we have three constructors for the class Test. One of them has no arguments, one has an integer argument and one has a string argument. This is how multiple constructors are organized inside a class.


From when Sunday is declared holiday?

when was Sunday declared as a public holiday


What are Java enums?

An enum, short for enumerated type, is a variable type that can only take on the values that are declared inside the enum declaration. An enum is declared like a class, except the word "class" is replaced by the word "enum", and the class body is replaced by a list of values that a variable of that type can take on. You can also include methods, instance variables, and constructors in an enum.


What is an construtor in c plus plus oops?

A constructor is a code segment that is invoked automatically whenever you instantiate an instance of a class. All non-primitive objects have at least one constructor but class designers can create as many constructors as required, giving consumers the freedom to construct objects more efficiently. If you don't declare any constructors, the compiler will generate both a default constructor (one that has no arguments) and a copy constructor for you. The purpose of a constructor is simply to initialise the class member variables. This is usually done via the class constructor initialisation list. Constructors also have a body (much like a function) which can be used to call methods of the class to perform more complex initialisations. However the initialisation list is the most efficient method, as it is not unlike initialising a primitive variable at the point of instantiation, rather than instantiating and initialising variables in two stages as you would with C. Constructors are also subject to access specifiers, but will usually be declared public unless the class is an abstract base class in which case they may be declared protected. Private access is typically used when declaring singleton classes (where construction is achieved via a static member method that instantiates the one and only instance of the class, which is itself declared a static member variable of the class). Note that copy constructors are invoked automatically whenever you pass objects to functions by value. Copying a derived object automatically invokes its base class copy constructors, while copying containers automatically invokes the embedded object copy constructors. This is why pass by reference (or via pointer) is the preferred method of passing objects into functions, as there is no need to reconstruct the object being passed.


For what purpose constructors are used in Java?

Constructors are used to create the instance of a class.


When was Worshipful Company of Constructors created?

Worshipful Company of Constructors was created in 1985.


Are constructors inherited?

Constructors, static initializers, and instance initializers are not members and therefore are not inherited.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Is there any friend constructor in c?

C is not an object-oriented programming language so there is no friend keyword let alone friend constructors. In C++, however, constructors can be declared friends. Consider the following code where the class Y default constructor has private access to X::foo() because the Y::Y() constructor is explicitly declared a friend of class X. Note that Y must be defined before X can be defined, thus X must be forward declared. #include<iostream> class X; // fwd declaration class Y { public: Y() { X x; x.foo(); } // X::foo is private, but Y::Y() is a friend. }; class X { friend Y::Y(); // friend constructor private: void foo() {} };


When you implement an interface method it should be declared as public in java?

This is not necessarily true. The only rules for this are that interface methods may not be private. They may be public, protected, or have the default (blank) access modifier.