Skip to content
Sharp

Object-oriented programming · interview question

Why can providing public setter methods for all fields in a class become a problem in production code?

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

Correct answer

D. It can lead to an invalid object state

Why

Public setters allow external code to modify fields without any validation, which can result in an object being put into an invalid state, violating encapsulation principles.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →