In object-oriented design, what is the purpose of a “constructor”? A. To clean up resources when an object is no longer needed.
B. To define the interface that a class will implement.
C. To initialize the state of an object at the time of its creation.
D. To inherit properties from a base class.
The correct answer is C. To initialize the state of an object at the time of its creation.
A constructor is a special method in a class that gets called when an object of that class is instantiated. Its primary purpose is to set up the initial state of the object by assigning values to its attributes. This ensures that the object is ready for use right after it’s created. If you have any further questions or want to explore this topic in more depth, feel free to check our extended services page!