2

How do I call a private method in this type of parent class? What I have done is an ugly peice of code but it works though I don't think it's a real solution.

Would really appreciate some advice on how this is done. I have looked to find solution but non that have given me some clarity on the matter.

    public abstract class Car {

         public void passItOn(int a) {

                takesVariable(a);

         }
    private void takesVariable(int a) {

         //Process the variables
    }

the child:

     public abstract class Wheel extends Car {

     boolean a = anotherMethod();
        if(value() != a) {
          passItOn(a);
        }

     }

There is some issue with my accaptance rate but I am on it! All will give + marks for replies!!!

Amar
  • 11,930
  • 5
  • 50
  • 73
user1501127
  • 865
  • 1
  • 18
  • 32
  • 7
    What's the point of having your method `private` if you want to use them? Better use `protected`, if you want them to be used in `derived` class. – Rohit Jain Dec 18 '12 at 16:16
  • use `protected` access modifier instead of `private`. Though your question isn't clear. – Amar Dec 18 '12 at 16:18
  • I agree with Rohit, if there is a reason not to do it -and why would there be one!-, I think your solution is ok – Ahmad Y. Saleh Dec 18 '12 at 16:19
  • protected works like a charm! Thanks a million! i knew there was something i was overlooking. I humble bow to your kung-fu! – user1501127 Dec 18 '12 at 16:22

5 Answers5

10

Accessing a private field/method is possible using reflection if the security manager allows it, but strongly discouraged.

Private methods are private for a reason. If you have access to the base class, making the method protected is a far better solution.

Kevin K
  • 9,344
  • 3
  • 37
  • 62
4

Private methods are not inherited, and so cannot be called. If you really want to have access to this method, change its access modifier keyword (protected or public).

jlordo
  • 37,490
  • 6
  • 58
  • 83
A--C
  • 36,351
  • 10
  • 106
  • 92
  • +1 you can only call private methods for classes in the same Java file with the same outer class. Otherwise `private` means only this class. – Peter Lawrey Dec 18 '12 at 16:40
  • Correct me if I'm wrong but private methods are also inherited, you just cannot call them in child class. This is because there is no error when you call public/protected method from parent class and they invoke private methods. – Tomasz Mularczyk Nov 23 '15 at 20:16
2

Do this if you are fine with making it protected:

public class BaseClass {
    protected void whatever() {}
}

Or this if you want to keep it private for some reason, and it will also give your base class more control over how/when "whatever" is used:

public class BaseClass {
    private void whatever() {}
    protected void doWhatever() {
        if(!forSomeReasonDoNotWantThisToBeUsed()) {        
            whatever();
        }
    }
}
Name
  • 2,037
  • 3
  • 19
  • 28
1

Make it protected if you want to reach it from subclasses.

Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
1

There is absolute no way this can be done. You cannot call a private method of a class from any other class(be it the extending class). It can only be accessed inside that class itself. There is a reason it is said to be private.

If you want it to be accessed by only the extending classes an not the outside world then use protected access modifier for your function.

Amar
  • 11,930
  • 5
  • 50
  • 73