• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Boyarsky, Selikoff: OCA Study Guide | Assessment Test - Q.8  RSS feed

 
Greenhorn
Posts: 8
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Namith Kumar
Greenhorn
Posts: 8
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!