1Z0-809 · Question #101
Which action encapsulates the Vehicle class?
The correct answer is D. Make the name variable private. Making the name variable private (D) directly implements encapsulation - the core OOP principle of hiding internal data so it can only be accessed through controlled methods, preventing external code from modifying the object's state arbitrarily. Why the distractors are wrong…
Question
Options
- AMake the Vehicle class public.
- BMake the name variable public.
- CMake the setName method public.
- DMake the name variable private.
- EMake the setName method private.
- FMake the getName method private.
How the community answered
(23 responses)- B4% (1)
- C4% (1)
- D78% (18)
- E13% (3)
Explanation
Making the name variable private (D) directly implements encapsulation - the core OOP principle of hiding internal data so it can only be accessed through controlled methods, preventing external code from modifying the object's state arbitrarily.
Why the distractors are wrong:
- A - Making the class
publiccontrols visibility of the class itself, not its internal data; encapsulation is about protecting fields, not class accessibility. - B & C - Making
nameorsetNamepublic does the opposite of encapsulation - it exposes internal state rather than hiding it. - E - A private
setNamewould prevent any controlled mutation, defeating the purpose of having a setter; encapsulation uses private fields with public methods. - F - A private
getNamewould hide the accessor, making the data inaccessible entirely - encapsulation restricts direct field access, not the methods that manage it.
Memory tip: Think of encapsulation as a capsule (like a pill) - the data (name) is the medicine sealed inside (private), and the methods (getName/setName) are the shell that controls what gets in and out.
Community Discussion
No community discussion yet for this question.