Skip to content
Sharp

Object-oriented programming · interview question

When designing an immutable class in Java, which principle should you follow to ensure encapsulation?

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

Correct answer

C. Do not provide setters for fields.

Why

To design an immutable class, do not provide setters for fields. This ensures that once an object is created, its state cannot be modified, maintaining encapsulation and protecting against unintended changes.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →