Answer the question
In order to leave comments, you need to log in
How to access a protected method of another class?
I want to use in my class a method of another class whose scope is protected. I do not inherit from him.
What's the best way to do it here? I suspect that changing the scope to public is not good.
Answer the question
In order to leave comments, you need to log in
If you don’t want to get into someone else’s code and change it, and even more so do intimate things through Reflection, then it’s best to create your own class inherited from the right one, and add a public method in your class that will call the protected ancestor.
Thus, it will not be necessary to change someone else's class, while maintaining access to all the functionality of the base class.
In fact, "change to public" is the only correct solution. And it’s even better to put the responsibility of this method in a separate class, since so many classes already depend on it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question