This prints DeerReinDeer,false.
I understand why it prints DeerReinDeer (fist invoke super() and then the sub class ReinDeer constructor). Can you help me understand why it prints the false ? Since the reference variable deer is pointing to the ReinDeer object , i was expecting it to invoke the hasHorns method in ReinDeer and print true. I know the fact that hasHorns is declared private in Deer has something to with it (i change that modifier to public and it did print true), but not sure why.
Hi the fact that hasHorns() method is private means that it's not visible outside Deer Class and it's not been inherited thus the ReinDeer's version is a regular method that only ReinDeer references have access too. So polymorphism doesn't apply in this case that's why Deer's version of hasHorn() method the one ending up being called. Hope it helps
Thanks Jean. Now i get it . Since reference variable deer is defined as type Deer (parent class) and the hasHorns method in Deer is declared as private , at compile time , this will get resolved to the hasHorns method of the parent Deer class. There will no chance of overriding since the method is declared private . It looks like JVM does not bother to check the run time reference if a similarly named method is present.