This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Upcasting doesn't work as intended; clarification needed  RSS feed

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have taken code from Sierra/Bates, end of chapter 2, and am playing around with it:



Output is:

furry bray
stripes bray

How do I make output such as:

furry generic noise
stripes bray


Thanks!
 
Creator of Enthuware JWS+ V6
Bartender
Posts: 3117
259
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandra,

You can't.

Two important rules apply here:
  • A reference variables type determines the methods that can be invoked on the object the variable is referencing
  • What method is called is decided at runtime based on the object type.

  • In your case the the method makeNoise() is allowed because the class of the reference variable Mammal has a method called makeNoise(). The actual method to be called is the method on the Zebra (because it overrides the one in Mammal)

    Regards,
    Frits
     
    Sandra Bachan
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Totally forgot about the overriding part; now I understand
     
    Sandra Bachan
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By the way, I noticed when I comment out makeNoise() in Zebra, it invokes makeNoise() in Mammal:




    Output:

    furry generic noise
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender
    Posts: 3117
    259
    Android Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    correct, now it just inherits the method from its super class.

    Regards,
    Frits
     
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sandra Bachan wrote:Hello,

    I have taken code from Sierra/Bates, end of chapter 2, and am playing around with it:



    Output is:

    furry bray
    stripes bray

    How do I make output such as:

    furry generic noise
    stripes bray


    Thanks!





    Output

    furry bray
    furry bray
    furry bray
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!