I have a output question in java.
package javaapplication32;
class Animal {
String name = "Animal";
Animal() {
System.out.println("Animal's name is: " + getName());
}
String getName() {
return name;
}
}
class Dog extends Animal {
String name = "Dog";
Dog() {
System.out.println("Dog's name is: " + getName());
}
String getName() {
return name;
}
}
public class JavaApplication32 {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
The output of the code is:
Animal's name is: null
Dog's name is: Dog
And my question are that;
why does the 'getName();' method which is in the Animal constructor invoke the method of Class Dog and why didn't it return "Dog" ?
I expected the output as;
Animal's name is: Animal
Dog's name is: Dog