• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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: 22656
126
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: 41124
848
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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic