Skip to content
Sharp

Object-oriented programming · interview question

Which technique can help maintain class invariants when encapsulating fields in a Java class?

Multiple choiceHardchapter-3-curatedpremiumoopencapsulation
Choose one answer
Answer and explanation

Correct answer

A. Using private fields with validation logic in setters

Why

By using private fields and incorporating validation logic within setters or update methods, a class can ensure that all changes adhere to its invariants, thus maintaining a consistent and valid 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 →