Bird
0
0

Why is it recommended to use getter and setter methods instead of making class fields public?

hard📝 Conceptual Q10 of 15
Java - Encapsulation
Why is it recommended to use getter and setter methods instead of making class fields public?
ATo avoid using constructors
BTo allow validation and control over data changes
CTo make code longer and complex
DTo prevent object creation
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation benefits

    Getters and setters allow validation and control when data changes.
  2. Step 2: Compare with public fields

    Public fields allow direct access without control or validation.
  3. Final Answer:

    To allow validation and control over data changes -> Option B
  4. Quick Check:

    Encapsulation = Control + Validation [OK]
Quick Trick: Getters/setters enable validation and protect data [OK]
Common Mistakes:
  • Thinking getters/setters only add code length
  • Confusing with constructors or object creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes