0

I need to call a private method present in Parent class and I have created a Child class that is extending Parent class. Now I want to call the private method which presents in Parent class by the object of Child class and without using reflection API. How can I do that?

And there is also permission that I can change the structure of Parent class.

class Parent{
  private void print(){
    System.out.println("Private Parent");
  }
}
class Child extends Parent{
}
Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Harshit Gupta
  • 719
  • 1
  • 9
  • 26
  • 7
    You seem to have misunderstood the meaning of [`private`](https://en.wikipedia.org/wiki/Access_modifiers). – khelwood Jun 27 '15 at 09:56
  • if you want to do it really then you can make another public method in parent to call it's print method – Madhawa Priyashantha Jun 27 '15 at 09:57
  • A private method of `Parent` can have delicate state that might be dependent on its `public` methods and allowing you to call the private method of `Parent` can wreck _havoc_. So for the sake of good what's `private` should be kept `private` ;) I you understand what I mean. – Narendra Pathai Jun 27 '15 at 10:01
  • Ask your friend what's the behavior in `Parent`'s private method that he/she wants to access? Think whether that behavior can be extracted in form of interface. – Narendra Pathai Jun 27 '15 at 10:03
  • Yes you are correct that a private method can only be accessed inside the class but is there any way to call the private method within the Parent class using the child object. – Harshit Gupta Jun 27 '15 at 10:04

4 Answers4

3

A private method can only be accessed from within the class (see, e.g., Java's tutorial about access control).

This rule can sometimes be circumvented by using reflection or bytecode manipulation, but in a straight forward way you simply cannot access a private method from a child class. If you have such a requirement, change it to be protected.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Yes you are correct that a private method can only be accessed inside the class but is there any way to call the private method within the Parent class using the child object. – Harshit Gupta Jun 27 '15 at 10:05
2

You cannot call a private method from the child class. The private method is only visible to the class itself. Change the method to protected or public to do so.

Llogari Casas
  • 942
  • 1
  • 13
  • 35
  • Yes you are correct that a private method can only be accessed inside the class but is there any way to call the private method within the Parent class using the child object. – Harshit Gupta Jun 27 '15 at 10:04
2

There is no way to call the private method within the Parent class using the child object except reflection or using inner class.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
0

The only way that I am aware you can do this - directly calling a private member of the superclass from a subclass - is if the subclass is nested inside the superclass. For example:

class Parent {
  private void parent() {}

  class Child extends Parent {
    private void child() {
      parent();
    }
  }
}

will compile. Note that if you wanted to make the Child class static, you'd have to make parent() static too.

Otherwise, you cannot do this - private means not accessible outside that class. You would need to make the method protected (or public) if you want it to be callable from subclasses. You might be well-served making it final too, in order to prevent it from being overridden in subclasses.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243