-1

I was just wondering how I would use something to get or use a private method from the super class in the sub class in java. I know that is a handful, but basically I want to know if I create a private method in a parent or super class, then what would be the code to bring in that private method into the sub class.

All help is greatly appreciated from this question.

NOTE:

I know that this question is similar to the question:

How can a derived class invoke private method of base class?

On this site Stack Overflow

However, the question answer for the question seem complex and hard to read for a beginner as well as not being the correct answer to the exact way that I am asking this question.

Community
  • 1
  • 1
  • Fix your design. If it's private you shouldn't be calling it. – user207421 Mar 08 '17 at 23:39
  • @EJP I know that it is a stupid scenario and you could easily just make that method public or even protected, but I just want to know if there is a way to do it – user7471732 Mar 08 '17 at 23:46
  • You can use reflection. – shmosel Mar 08 '17 at 23:46
  • Yes, it is a really bad idea, and yes, there is a way to do it. See http://stackoverflow.com/questions/11483647/how-do-i-access-private-methods-and-private-data-members-via-reflection – Andy Thomas Mar 08 '17 at 23:46
  • @shmosel what is reflection – user7471732 Mar 08 '17 at 23:47
  • See the link above and [this tutorial](https://docs.oracle.com/javase/tutorial/reflect/). – shmosel Mar 08 '17 at 23:48
  • @AndyThomas thank you very much! This looks helpful, I don't mind going through that site; however, is there just an easier way of explaining so that I could try to figure it out myself without just getting the answer feed to me through the site with an example – user7471732 Mar 08 '17 at 23:48
  • @shmosel, sorry, I dodn't see the tutorial part was in blue based on my color blind issues. I thought that you were referencing the link above as a tutorial. That is why I edited the question and commented what I had earlier – user7471732 Mar 08 '17 at 23:54
  • @user7471732 - The short form is that 1) access control like `private` is there to prevent you from making mistakes in normal code, and 2) reflection allows you to inspect classes and objects without enforcing access control. To use reflection, you'll have to dive into the details, either through this site or a tutorial or a reference. – Andy Thomas Mar 08 '17 at 23:57
  • @AndyThomas Okay, so your basically ignoring the rules that make something private, public, or protected – user7471732 Mar 09 '17 at 00:13
  • @AndyThomas That should be enough. Thank you for all of your help – user7471732 Mar 09 '17 at 00:13
  • @AndThomas Also, I am sorry that I even made this question. If I had scene the link for the question that you linked about reflection. I wouldn't have posted that question. However, when I searched my question up and when I was typing the question. The question that you should be did not come up under the related or suggested questions. – user7471732 Mar 09 '17 at 00:14
  • Thank you everyone for all your help – user7471732 Mar 09 '17 at 00:16

1 Answers1

3

I want to know if I create a private method in a parent or super class, then what would be the code (in the form of an accessor or mutator method) to bring in that private method into the subclass.

You can't. That's what private means. Make it protected instead.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • No, I know how you would do it for protected. But, my teacher must have mispoke then. Sorry – user7471732 Mar 08 '17 at 23:40
  • Are you sure you want to access a private *method* and not a private *variable*? – D M Mar 08 '17 at 23:41
  • No, I know that there some way to access a private variable from reading other questions on this site. That is not what I am looking to do though. – user7471732 Mar 08 '17 at 23:44