aspose file tools*
The moose likes Beginning Java and the fly likes help on HFJ exercise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help on HFJ exercise" Watch "help on HFJ exercise" New topic
Author

help on HFJ exercise

vidu sri
Greenhorn

Joined: Jun 15, 2009
Posts: 8


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.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Because vampire's frighten method is overloaded, not overridden. The monster class doesn't know anything about that method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93

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.

Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help on HFJ exercise