• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance constructor -book head first example  RSS feed

 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.print(hallo forum);

HI everyone,
I am stuck since some days on the below code, as cannot understand what the compiler does with the code I attached below here is my flow compiler idea:

1) Find main, and run the code testHippo
2)create an object h that refer to hippo
3)?
4)?
5)?
should be enough three steps after, would be not fair to ask you more
my idea is 3) goes up to class Hippo that extends Animal so goes up to Animal 4) nothing happens with Animal on the stack and goes back to Hippo 5) execute super(name)???that should go up again to what? when the command "System.out.println(h.getName());" will be executed?

thank you for your time

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super(name) is a constructor call. It calls the constructor in the super class (Animal) that takes a single String parameter.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:super(name) is a constructor call. It calls the constructor in the super class (Animal) that takes a single String parameter.

thank you Mike,
but unfortunately still cannot understand how the compiler processes the information from declaring h to System.out. print.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) you call the constructor on Hippo, passing the name as a parameter.

2) The Hippo constructor calls the Animal constructor passing the same name parameter.

3) The Animal constructor stores the name in the field in Animal.

4) You call getName(), and this returns the name from Animal which was set in the constructor.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thank you very much, it is exciting I think I could get it

Mike. J. Thompson wrote:


1) you call the constructor on Hippo, passing the name as a parameter.

2) The Hippo constructor calls the Animal constructor passing the same name parameter.

super clear!
Mike. J. Thompson wrote:
3) The Animal constructor stores the name in the field in Animal.


If I am right are two sub steps: 3.a Animal constructor in class Animal takes the string and associate to name and.. 3.b being the row "private string name;" in the class where we just gave value as name, the compiler associates name to the string Buffy and....really important for me to understand... goes back to the initial class testHippo to execute the second row where is h.getname, right?
Mike. J. Thompson wrote:
4) You call getName(), and this returns the name from Animal which was set in the constructor.

it is a metod getter that return the value name that allows to print back to the screen the value Buffy
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to visualize it, try putting a System.out.println before and after every statement. This will show you the order things run.

Note that you can't put anything before the call to super(), that must come first in the constructor.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:If you want to visualize it, try putting a System.out.println before and after every statement. This will show you the order things run.

Note that you can't put anything before the call to super(), that must come first in the constructor.


OK
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!