Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Polymorphism Problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys ,
I have a problem with following code and output.


When you run above you get the output as follows

Your choice: This is a A
Woof Woof

Your choice: This is a Cat
It's C the D
Miiaooww

Problem here is how do we get he 'It's C the D' line in the output. It seems it is coming from Constructors.
But in Cat class 'toString' method is not inside the constructor.
can some one explian this?

/Rashmi
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the Cat toString, first you call the superclasses's to string. So it prints This is a Cat and then Its's C the D. Constructors assign the values that you pass to the instance variable.

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are overriding the toString() method in the Cat class and return the name and the breed of the cat, as passed in the constructor.



petChoice1 is an animal, the toString() method is invoked and since the implementation is Cat it is casted and to Cat's toString() is invoked.
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your replies. Still there is somethig which I dont understand.
I understand that Cat class has override toString method.
When Cat class is created it calls Super constructor , which is Animal.
To my understanding Animal Constructor is only

Am I wrong? All it does it assigning the type.

So how does the 'toString' method gets activated?
Please explain

/Rashmi.
 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So how does the 'toString' method gets activated?


If you put any reference type inside System.out.println() It tries to call toString() method of that object type inside that reference. Unless that reference type is null, you will get some String as output.

Now coming back to your question


You got following output

Your choice: This is a Cat
It's C the D


because On line1 you are first called super class's toStrng() method inside Cat's toString() method.

Regards

 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally only the cat's tostring is called. Try your program removing super.toString() from your Cat's toString().
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
Thanks for these explanations.
It is clear now.

/Rashmi
 
pie. tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic