| Author |
I need to help with the question 13 chapter 2
|
Mauricio Archbold Babroza
Greenhorn
Joined: Apr 01, 2009
Posts: 11
|
|
why is the result "furry bray" and is not "furry generic noise"???
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
Because members variables never inherited and do not behave in polymorphic way.
Both the classes have variable with same name and you are assigning a child reference to parent object. So the instance member of the referring (Parent class) class will be called and method of the original underlying class (Child class) will be called.
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2726
|
|
Mauricio Archbold Babroza wrote:I need to help with the question 13 chapter 2
Is it from K&B Study Guide? Please quote the source properly.
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
pankaj vijay
Ranch Hand
Joined: Apr 01, 2008
Posts: 75
|
|
there is one very simple rule about this: Methods are called based on object while static methods & members variables are called based on reference.
Now you wrote
here reference is of Mammal while object is of Zebra so
will call Mammal variable &
will call zebra noise method
|
Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
will call Mammal variable &
view plaincopy to clipboardprint?
m.name will call zebra noise method
i think you want to say
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Yep, for sure. I changed makeNoise() to static in both classes,
as demanded by the compiler, and the output became, "furry
generic noise." Mauricio Archbold Babroza, are you there?
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
 |
|
|
subject: I need to help with the question 13 chapter 2
|
|
|