1Z0-809 · Question #223
Given the definition of the Book class: public class Book { private int id; private String name; public Book(int id, String name) {this.id = id; this.name = name;} public int getId() { return id; }…
The correct answer is A. It demonstrates encapsulation. Option A is correct because the Book class hides its fields (id and name) using the private access modifier, exposing them only through controlled public getter and setter methods - this is the definition of encapsulation: bundling data with the methods that operate on it and…
Question
Options
- AIt demonstrates encapsulation.
- BIt is defined using the factory design pattern.
- CIt is defined using the singleton design pattern.
- DIt demonstrates polymorphism.
How the community answered
(30 responses)- A73% (22)
- B17% (5)
- C3% (1)
- D7% (2)
Explanation
Option A is correct because the Book class hides its fields (id and name) using the private access modifier, exposing them only through controlled public getter and setter methods - this is the definition of encapsulation: bundling data with the methods that operate on it and restricting direct access. Option B is wrong because the factory pattern involves a separate factory class or method that creates objects; here the constructor does it directly. Option C is wrong because the singleton pattern restricts a class to a single instance (typically via a private constructor and a static getInstance() method), which Book does not do. Option D is wrong because polymorphism requires either method overriding (inheritance) or method overloading to allow one interface to represent multiple types - none of that is present here.
Memory tip: Think "encapsulation = capsule." Just like a pill capsule hides its contents and controls what gets in or out, encapsulation hides fields (private) and controls access through getters/setters (public). If you see private fields + public get/set methods, that's your capsule.
Community Discussion
No community discussion yet for this question.