Skip to content
Sharp

Object-oriented programming · interview question

Why is it beneficial to use getters and setters in Java classes?

Multiple choiceMediumchapter-2-curatedpremiumoopinstance-methods
Choose one answer
Answer and explanation

Correct answer

C. They allow for controlled access to the class's fields.

Why

Getters and setters provide a controlled way of accessing and modifying a class's fields, supporting encapsulation and protecting the data from unintended changes.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →