Skip to content
Sharp

Core Java · interview question

You notice that a Java class uses a mix of public fields and methods. What refactoring strategy can improve its encapsulation?

Multiple choiceHardchapter-3-curatedpremiumcore-javacode-reading-and-state-safety
Choose one answer
Answer and explanation

Correct answer

B. Encapsulate fields using private access and provide controlled access through methods.

Why

Encapsulating fields by making them private and providing controlled access through getter and setter methods ensures that the internal state is not exposed directly, improving encapsulation and maintaining control over how the fields are accessed and modified.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →