Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need to help with the question 13 chapter 2

 
Mauricio Archbold Babroza
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is the result "furry bray" and is not "furry generic noise"???

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4681
329
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic