• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 26
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic