Operation types: Is an operation that alters the state of an existing object called a “constructor operation,” or is that term reserved for creating and initializing new instances?

Difficulty: Easy

Correct Answer: Incorrect

Explanation:


Introduction / Context:
This question distinguishes between two common operation categories in OO design: constructors and mutators. Mislabeling these can blur the life cycle of objects and lead to misunderstandings in both design documentation and code reviews.


Given Data / Assumptions:

  • The statement asserts that an operation which alters the state of an object is a “constructor operation.”
  • We assume mainstream OO languages and UML usage.
  • We distinguish initialization at creation time from subsequent state changes.


Concept / Approach:
A constructor is an operation invoked when a new object is created; it initializes the new instance’s state. After creation, operations that change an object’s state are typically called mutators, setters, or commands. Therefore, calling any state-altering operation a “constructor” is incorrect: constructors are specifically about creation and initial setup, not ongoing mutations of existing objects.


Step-by-Step Solution:

Define constructor: operation executed once at instantiation to initialize state.Define mutator: operation that modifies state after the object already exists.Recognize the statement conflates these roles.Conclude that the statement is incorrect.


Verification / Alternative check:
Inspect languages like Java, C++, C#, or Python: constructors (or init) run at creation; mutator methods (for example, setBalance, applyDiscount) change state later. UML mirrors this practice in sequence and class diagrams.


Why Other Options Are Wrong:

  • “Correct” misuses terminology.
  • Prototype-based or manual memory management does not redefine “constructor.”
  • Immutability eliminates mutators but still does not repurpose constructors for later state changes.


Common Pitfalls:
Treating heavy initialization as general mutation; forgetting to keep constructors side-effect free beyond initialization, which complicates testing and invariants.


Final Answer:
Incorrect

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion