-5

I've read several conflicting sources online about whether or not private methods can be overridden. What exactly is going on in this example then? Is method move being hidden?

class Animal {
   private void move() {
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog {
   public static void main(String args[]) {
      Dog b = new Dog();   // Dog reference and Dog object
      b.move();   // runs the method in Dog class
   }
}

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html says that it's not technically overriding, so what is this called and will there be any unexpected behaviours?

JobHunter69
  • 1,706
  • 5
  • 25
  • 49
  • 1
    `Animal reference but Dog object` but it seems to be `Dog` reference – Nitin Bisht Jul 06 '20 at 17:12
  • @NitinBisht Typo, fixed, thanks – JobHunter69 Jul 06 '20 at 17:13
  • @Ivar no i'm asking about hiding not overriding – JobHunter69 Jul 06 '20 at 17:16
  • @Goldname What specifically do you mean by hiding then? It is hidden isn't it? Did you manage to get "Animals can move" to your console somehow? – Ivar Jul 06 '20 at 17:17
  • @Ivar Hiding in this sense https://stackoverflow.com/questions/10594052/overriding-vs-hiding-java-confused – JobHunter69 Jul 06 '20 at 17:22
  • @Ivar Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass. – JobHunter69 Jul 06 '20 at 17:24
  • From here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html – JobHunter69 Jul 06 '20 at 17:24
  • @Goldname That answers your question than right? Hiding and overriding don't apply to private methods. Therefor your question "_Can private methods be hidden?_" doesn't make much sense. It is already hidden, because there is no way you can access it from outside of the `Animal` class. There is no relation what so ever between the move function in your Animal class and the one in your Dog class. They're just two different functions in two different classes. – Ivar Jul 06 '20 at 17:57

2 Answers2

0

What you have done is not overriding. If you want to confirm this, just put @Override before public void move() in the class Dog and you will see compilation error.

Note: A private method is always hidden inside a class. There is no way to make it accessible outside the class.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Right, I'm asking about hiding not overriding – JobHunter69 Jul 06 '20 at 17:15
  • 1
    That won't help until access of `move()` is changed in Animal to `public` – Nowhere Man Jul 06 '20 at 17:16
  • @AlexRudenko - I didn't get you. – Arvind Kumar Avinash Jul 06 '20 at 17:17
  • @ArvindKumarAvinash My comment was related to your initial version. – Nowhere Man Jul 06 '20 at 17:20
  • @AlexRudenko - The update didn't change any meaning. I just added `and you will see compilation error.` to make it a bit clear but the meaning remains same. – Arvind Kumar Avinash Jul 06 '20 at 17:22
  • What I was reading was this: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html. – JobHunter69 Jul 06 '20 at 17:26
  • What does it mean by it's technically not hiding? If so, then what tam I doing? – JobHunter69 Jul 06 '20 at 17:26
  • hiding may be applied to fields and `static` (class) methods. – Nowhere Man Jul 06 '20 at 17:29
  • It says on that website: Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass. – JobHunter69 Jul 06 '20 at 17:33
  • @Goldname - This is absolutely correct. What it means is: a method with the same signature in the subclass can not override or hide the one marked `private` in the superclass. In a simple term, the subclass does not have any way to change the implementation/meaning of the `private` method in the superclass. – Arvind Kumar Avinash Jul 06 '20 at 17:37
0

You have to write your Test like this..

public class TestDog {
   public static void main(String args[]) {
      Animal b = new Dog();   // Animal reference but Dog object
      b.move();   // runs the method in Dog class
   }
}

Then, you get:

/TestDog.java:16: error: move() has private access in Animal
      b.move();   // runs the method in Dog class
       ^
1 error

Your test case simply calls the move() in the Dog and it isn't overriden.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97