Skip to content
Sharp

Object-oriented programming · interview question

How does using private fields in a class contribute to encapsulation?

Multiple choiceMediumchapter-3-curatedpremiumoopdefensive-copying
Choose one answer
Answer and explanation

Correct answer

A. By restricting direct access to the fields from outside the class

Why

Using private fields in a class restricts direct access from outside the class, which is a key aspect of encapsulation. It allows the developer to expose only what is necessary and control how internal data is accessed and modified.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →