• 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

help on HFJ exercise

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if object is created for both the classes and method is called,why the output is arrrgh arrrgh instead of
arrrgh and a bite?.

i could understand that the arguement differs and byte could not hold int,but still could not guess why the input is as like stated above.


 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because vampire's frighten method is overloaded, not overridden. The monster class doesn't know anything about that method.
 
author & internet detective
Posts: 41855
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vidu,
Let's walk through what the JVM "thinks" starting with the loop.
1) First iteration. I see you have this monster object and want to call frighten() on it with an int.
2) I see that method - I'll call it and print out "arrrgh".
3) Second iteration. I see you have this vampire object and want to call frighten() on it with an int.
4) I'll start by looking for matches in the subclass. Vampire has a frighten() method. I can't use it though since I have an int to pass and vampire only takes in a byte.
5) Moving on to the superclass - monster. Now I have a match.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if object is created for both the classes and method is called,why the output is arrrgh arrrgh instead of
arrrgh and a bite?.

i could understand that the arguement differs and byte could not hold int,but still could not guess why the input is as like stated above.




See in 1st case you are creating an instance of monster and in 2nd case you creating instance of child class and assigning to parent class which is known as Polymorphism, therefore you are getting the same result in both cases. It means in the both cases the frighten method of monster class get called.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic