I know that constructors are not inherited. But the following mock exam question has me confused. Can someone explain the reason for the answer?
Given the following class definition
class Mammal{
Mammal(){
System.out.println("Mammal");
}
}
class Dog extends Mammal{
Dog(){
System.out.println("Dog");
}
}
public class Collie extends Dog {
public static void main(
String argv[]){
Collie c = new Collie();
}
Collie(){
this("Good Dog");
System.out.println("Collie");
}
Collie(String s){
System.out.println(s);
}
}
What will be output?
1) Compile time error
2) Mammal, Dog, Good Dog, Collie
3) Good Dog, Collie, Dog, Mammal
4) Good Dog, Collie
--
The answer is 2. Actually, I guessed it right, but why isn't the answer 4? If constructors are not inherited, why are the parent constructors invoked instead of just the constructor for Collie?