LearnFrenzy
Sign in Take a free test

Encapsulation · Encapsulation MCQ · Question 4

Why put validation in a setter? Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the main reason to validate inside a setter?

  • A Speed
  • B To stop invalid values from ever being stored in the object Correct
  • C To make the field public
  • D Because the compiler requires it
Correct Answer: Option B

Every change goes through the setter, so one check protects the whole program.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links