Originally posted by cairo cairo:
... Why and how this superclass (Animal) knows to call its subclass (Dog)'s method, sayHello()? and print out "Dog says Hello" and not "Animal say Hello"? ...
This is
polymorphism. The method sayHello() is defined in Animal, but it's also overridden in Dog. Because
the true runtime type of the instance is Dog, the method body in Dog is invoked. Note that this would happen even if the
reference type was Animal, for example...
Animal myAnimal = new Dog();
myAnimal.sayHello(); //Output: "Dog says hello."
One way to avoid this dynamic binding (associating the method body with the runtime type) is to make the methods static in both the super and subclass. This way, the method is not overridden, and is invoked based on the reference type instead of the runtime type. Another way is to make the method private in the superclass so that it's not inherited. You can also prevent overriding by making the method final in the superclass.
Note that within Dog, you can invoke the Animal version of sayHello() by calling super.sayHello().