0

Hello I have class with private method, and I need override it by reflation. I see many examples fro change private fields, or call private methods, but I don't found any override example.

I have this code

public class TestClass {
public void print(string str)
{
doSomeThing(str);
}

private void doSomeThing(string str)
{
Log.d("tag","printed :"+str);
}
}

but I want change Log.d("tag","printed :"+str); to Log.d("tag","printed override :"+str); or other code. How is it possible do with reflection?

Nikolay
  • 344
  • 1
  • 15

1 Answers1

0

You cannot override a private member, because a private member belongs to its unique class. If another class extends that class, the new class would not have such member, and therefore you can just create a new one.

Maybe you want to use the protected modifier instead of private?

Ssr1369
  • 348
  • 3
  • 16
  • Class where I want override used in other library that I cannot change. – Nikolay Oct 29 '19 at 11:43
  • Do not override then, just create the method. It's private, therefore by using inheritance you're not including that method. Your new object will not have that method, unless you create it. – Ssr1369 Oct 29 '19 at 11:46
  • Ok if it's will be public or protected is it possible override with reflection? (I cannot override in normal way using `extends` ) – Nikolay Oct 29 '19 at 11:48
  • Yes, if you use protected you should be able to override it using the normal way 'extends'. – Ssr1369 Oct 29 '19 at 11:56
  • In what way? If I have ClassA, that based on BaseClass(where I need add change), but I cannot change extends for ClassA – Nikolay Oct 29 '19 at 13:48
  • Make the method protected in BaseClass, and then override it in ClassA. – Ssr1369 Oct 30 '19 at 12:06
  • I cannon change anything in ClassA – Nikolay Oct 30 '19 at 15:06
  • Then what you want to do is not called override. You seem to be trying to create some sort of polymorphic class while also not being able to modify ClassA. There's not much I can help you with on the current situation, maybe you could try explaining your needs more clearly? – Ssr1369 Oct 30 '19 at 15:08
  • Ok. I create plugin for library that contains class that extends `WebView`. In `WebView` (or in `BaseWebView` in library) I need override method `loadDataWithBaseURL`. My idea that my plugin will agnostic, and didn't contains library code – Nikolay Oct 31 '19 at 14:24