Skip to content
Sharp

Reflection API · interview question

When using Reflection to access a private field in a superclass, which method should you use?

Multiple choiceHardchapter-16-curatedpremiumreflection-apibreaking-encapsulation
Choose one answer
Answer and explanation

Correct answer

A. GetDeclaredField()

Why

To access a private field in a superclass, 'getDeclaredField()' should be used, as it allows access to any field declared in the class, regardless of its visibility, unlike 'getField()' which only accesses public fields.

Test yourself on Reflection API

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

Start free test
All Reflection API questions →