Skip to content
Sharp

Reflection API · interview question

How can a mini-framework dynamically invoke a private method 'calculateDiscount' in an e-commerce application?

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

Correct answer

A. Use getDeclaredMethod, setAccessible(true), and invoke

Why

To dynamically invoke a private method, you retrieve it using getDeclaredMethod, call setAccessible(true) to bypass access checks, and then use invoke to execute the method.

Test yourself on Reflection API

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

Start free test
All Reflection API questions →