Bird
Raised Fist0

A developer writes a class with private fields but no getters or setters. What is the main problem with this design?

medium🐞 Bug Identification Q7 of Q15
OOP & Design Patterns - Encapsulation - Data Hiding, Getters/Setters & Access Modifiers
A developer writes a class with private fields but no getters or setters. What is the main problem with this design?
AExternal code cannot access or modify the private fields at all
BPrivate fields become accessible globally by default
CThe class automatically generates default getters and setters
DThe private fields are exposed as public by inheritance
Step-by-Step Solution
Solution:
  1. Step 1: Analyze private fields without accessors

    Without getters/setters, private fields are inaccessible to external code, limiting usability.
  2. Final Answer:

    Option A -> Option A
  3. Quick Check:

    Private fields need accessors for external interaction [OK]
Quick Trick: No getters/setters means no external access [OK]
Common Mistakes:
MISTAKES
  • Assuming private fields become global
  • Believing accessors are auto-generated
  • Thinking inheritance exposes private fields
Trap Explanation:
PITFALL
  • Candidates mistakenly think private fields are accessible without accessors.
Interviewer Note:
CONTEXT
  • Checks if candidate understands necessity of accessors for usability.
Master "Encapsulation - Data Hiding, Getters/Setters & Access Modifiers" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes