Skip to content
Sharp

Object-oriented programming · interview question

In Java, how can you enforce an invariant in a class?

Multiple choiceMediumchapter-3-curatedpremiumoopcontrolled-mutation
Choose one answer
Answer and explanation

Correct answer

A. By using private fields and controlled access methods.

Why

Enforcing invariants involves using private fields and providing controlled access methods (like getters and business logic methods) that maintain the consistency of the object's state.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →