In which access should a constructor be defined, so that object of the class can be created in any function?
Options
- A. Protected
- B. Private
- C. Public
- D. Any access specifier will work
C. Public
To create an object of a class from any function, the constructor must be accessible everywhere.
Public constructor → Object can be created from anywhere.
Private constructor → Object cannot be created outside the class (used in Singleton pattern).
Protected constructor → Object can only be created inside the class or by derived classes.
Any access specifier → ❌ Incorrect, because access level affects object creation.
For example, in C++ or Java, if the constructor is declared public, objects can be created in any function like main() or other methods.
Attempt Quiz Now:
BPSC Tre 4.0 OOPS Concepts Practice Set