Java: Encapsulation
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 →Quick study sessions to strengthen memory and retain key concepts.
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 →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 →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 →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 →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 →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 →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 →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 →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 →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 →