Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using super keyword and the calling order in this example  RSS feed

 
Jennifer Schwartz
Ranch Hand
Posts: 46
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • 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.



 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, totally got it now! Thanks so much! Glad to put this one to rest.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!