Flashcards

Quick study sessions to strengthen memory and retain key concepts.

Flashcard Study

Java: Defensive Copying

Front

How does List.copyOf enhance encapsulation?

List.copyOf creates an unmodifiable copy of a list, preventing external modifications. Using List.copyOf ensures that any attempt to…

View Card →
Flashcard Study

Java: Defensive Copying

Front

Why is encapsulation a key principle in Java?

Encapsulation protects an object's internal state by restricting access to its internal components. Encapsulation allows changes to the…

View Card →
Flashcard Study

Java: Defensive Copying

Front

Why use getter methods instead of making fields public?

Getter methods allow controlled access and validation. Using getter methods instead of public fields provides an opportunity to…

View Card →
Flashcard Study

Java: Defensive Copying

Front

How would you explain a common pitfall of exposing mutable class fields in an interview?

Exposing mutable fields can lead to external unintended modifications. Exposing mutable fields directly allows external code to alter…

View Card →
Flashcard Study

Java: Defensive Copying

Front

Why is it crucial to validate inputs when setting internal collections?

Validation prevents invalid or harmful data from affecting the collection. Validating inputs ensures that only appropriate data is…

View Card →
Flashcard Study

Java: Defensive Copying

Front

What design choices help prevent collection leaks?

Use unmodifiable views or defensive copies when exposing collections. To prevent collection leaks, avoid exposing internal collections directly.…

View Card →
Flashcard Study

Java: Defensive Copying

Front

How does the private keyword support encapsulation?

Private fields restrict access to within the class, enforcing encapsulation. Using the private keyword on fields ensures that…

View Card →
Flashcard Study

Java: Defensive Copying

Front

Why does returning a reference to a mutable internal collection change runtime behavior?

It allows external code to modify the collection, violating encapsulation. Returning a reference to a mutable collection can…

View Card →
Flashcard Study

Java: Defensive Copying

Front

How does defensive copying protect object integrity?

Defensive copying prevents external changes by returning copies of mutable objects. When exposing a mutable object, returning a…

View Card →
Flashcard Study

Java: Defensive Copying

Front

What does it mean for an object to be immutable?

An immutable object cannot be altered after its creation. Immutability means the object's state cannot change once it…

View Card →