• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

polymorphism

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is a code I ran on my machine got output


But Kathy in your book the output given is


Where is the problem?
Theorotically your output looks correct. Is it a problem in my JVM version?

Thanks in advance.
Prasanna.
[ April 18, 2005: Message edited by: Prasanna ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Sampras", while you are waiting for an answer to your question, please take time to read our JavaRanch Naming Policy and change your displayed name to conform with it. In your case we need a name in the format <first name> <family name> (in that order).
Thanks,
-Barry
[ April 18, 2005: Message edited by: Barry Gaunt ]
 
Prasanna Phatak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that ok Barry?

Sorry. I didn't read the policy carefully.

Regards,
Prasanna.
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change the third and fourth line in the main function to



and you will get the excpected result.

See, when i = 1, you are essentialy doing f = b. So, from i = 2 onwards getFoo always returns b
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your output is correct. In your for loop If i value is 0 then method getFoo() returns f ie.,Foo class reference. So its override superclass method react and print "Foo".

If I = 1 getFoo() method return subclass reference b and initialized into super class object f. So it override subclass method react() and display Bar.

If f=2 the getFoo() method returns reference f. but this time f has subclass refernce. bcoz it is initialized in for loop (ie, if I==1). so again and again it executes subclass method react().

So your output is exaclty correct
 
Prasanna Phatak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raghu.

I got the point.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s that ok Barry?

Sorry. I didn't read the policy carefully.


Still, you have only 1 single name, while Barry told you to have <first name> <family name> .

Nick
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Nicholas pointed out we do require the form <first name> <family name>, not one name as you currently have.



The result "Foo Bar Foo Bar Foo Bar" can be obtained by replacing the use of the variable f by the variable f2 in the for-loop of the main method. So I guess someone got a little mixed up with what example when with which output.
 
You showed up just in time for the waffles! And this 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