Flashcards

Quick study sessions to strengthen memory and retain key concepts.

Flashcard Study

Java: Encapsulation

Front

Why should fields in a Java class be declared private?

To control access and protect the internal state. Declaring fields as private ensures that only methods within the…

View Card →
Flashcard Study

Java: Encapsulation

Front

What strategy ensures a class remains immutable?

Make fields final and provide no setters. Frame the concept in practical terms so you can explain it…

View Card →
Flashcard Study

Java: Encapsulation

Front

Why avoid public fields in a well-designed class?

To ensure secure and controlled access. Frame the concept in practical terms so you can explain it during…

View Card →
Flashcard Study

Java: Encapsulation

Front

How can you safely allow access to a list of orders in an e-commerce system?

By returning a copy or an unmodifiable view. To prevent external modification of a private list, you can…

View Card →
Flashcard Study

Java: Encapsulation

Front

Why does getter methods in encapsulation matter in this design?

To provide controlled access to fields. Frame the concept in practical terms so you can explain it during…

View Card →
Flashcard Study

Java: Encapsulation

Front

Why should objects protect their own state?

So outside code cannot push the object into invalid business state. Encapsulation matters because a store model should…

View Card →
Flashcard Study

Java: Defensive Copying

Front

How can you avoid collection leaks in a class design?

Avoid leaks by using private fields and returning copies or unmodifiable views. To prevent collection leaks, keep collection…

View Card →
Flashcard Study

Java: Defensive Copying

Front

How would you explain the tradeoff of using unmodifiable views in an interview?

Unmodifiable views prevent modification but can reflect changes to the underlying collection. While unmodifiable views prevent direct modification,…

View Card →
Flashcard Study

Java: Defensive Copying

Front

Why is defensive copying crucial for internal structures?

Defensive copying prevents external changes by providing copies of mutable objects. By returning a copy of a mutable…

View Card →
Flashcard Study

Java: Defensive Copying

Front

What risks do mutable collections pose if shared directly?

Sharing mutable collections directly can lead to unintended modifications and data inconsistencies. If a class exposes a mutable…

View Card →