• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

using super keyword and the calling order in this example

 
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys, I'm really hoping you can help me out with this one because I'm at the end of my rope in trying to understand the following:



I am confused about what the output of this should be...my study guide shows:
animal
black
animal
black

I am guessing that Pony is called in the main method first (but how it returns Animal is beyond me). Then within the Pony class, the superclass constructor is called (Horse) and passed the string "black". Third, comes the string "animal" (from the Animal constructor of the Animal class). And lastly, the "new Horse" that was assigned the string "black".

I'm sure this is ALL totally wrong, but can someone please explain the order in which this is executed in the simplest terms possible. Also, what is the purpose of the Pony() constructor in the Pony class? and are all of these constructors implicitly declared as super?
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each constructor either calls another constructor from the same class (using this(...)) or a constructor from the super class. If you don't add a call to this(...) or super(...) yourself the compiler adds a call to super().

So let's investigate your code.
You create a Pony object. The constructor first calls the Horse(String) constructor. This in turn first calls the Animal() constructor because of the implicit super(). Therefore, "animal" is printed. The Horse(String) constructor now continues and prints the type, "black".
Next, the Pony constructor continues. This creates another Horse, so again the Animal() constructor is called (printing "animal") followed by the remainder of the Horse(String) constructor, and again the type ("black") is printed.
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, this was a bit confusing. I do understand the use of the implicit super(). What I don't get is how do you know where to start calling these constructors? and this may be a dumb ?, but hey..that's why I'm here. IS THIS.. in the main method not used?

Also, is the Horse constructor that has the String parameter of "type" being overloaded with the other constructor's param of "black"?

Just trying to tear this thing apart to understand..thanks.



 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That line is used - everything starts in the main method.

- Line 18 creates a new Pony
- that calls the Pony() constructor
- which calls the Horse(String) constructor
- etc, as Rob described

What's going on there isn't "overloading". Overloading refers to the case where you have more than one method with the same name, but with different argument types, which isn't happening here. What you do have is the Pony constructor calling the Horse constructor with the argument "black".
 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, totally got it now! Thanks so much! Glad to put this one to rest.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic