0

I am extending example.java, and in this class there is a private method, exampleMethod(). Now, I want to have the same method in my class secondexample.java and call super.exampleMethod(). Since exampleMethod() in example.java is private I can't! How would I go about doing this?

Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    It's likely private for a good reason. You won't be able to override it but you could call it using reflection. – Paul Abbott Nov 17 '14 at 20:17
  • You can find the answer here. [http://stackoverflow.com/questions/14398157/how-to-invoke-parent-private-method-from-child][1] – Vikas Mangal Dec 06 '14 at 11:11

1 Answers1

0

You can't call a private function from a subclass. One thing you can do is change it to a protected method. You can check this post for more about modifiers.

Community
  • 1
  • 1
benji
  • 606
  • 6
  • 12
  • I should have been more clear in the post. I know about modifiers, but the class I am extending is from an API, so I can't edit its' contents. – FiskFille Nov 17 '14 at 20:24
  • If the `API` made the function `private` then they do not want others to change or call it. Modifiers are there for access control. However if you really need to call that method then you can change the modifier, even without having the source. Javas `Reflection` API allows you to change classes and everything at runtime. See here: https://stackoverflow.com/questions/880365/any-way-to-invoke-a-private-method – Zabuzard Jun 28 '17 at 22:04