jeudi 6 juin 2019

Why is 'Class::getMethods' returning only methods from the Object class and methods named "wait"? [duplicate]

This question already has an answer here:

I'm learning about reflection and am testing the "Class::getMethod" functionality and am getting strange behavior that I would appreciate an explanation for.

public class Animal
{
  protected void doAnimalStuff() {
    System.out.println("animal stuff");
  }
}

public class Dog extends Animal {

  @Override
  protected void doAnimalStuff() {
    System.out.println("dog stuff");
  }

  protected void bark() {
    System.out.println("woof");
  }

}

public static void main(String[] args)
  {
    Arrays.stream(new Dog().getClass().getMethods()).forEach((method) -> System.out.println(method.getName()));
  }

I would expect the output to be something along the lines of:

doAnimalStuff
bark
getClass
hashCode
equals
notifyAll
toString
notify

but the actual output is:

getClass
wait
wait
wait
hashCode
equals
notifyAll
toString
notify





Aucun commentaire:

Enregistrer un commentaire