wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Contructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Contructors" Watch "Contructors" New topic
Author

Contructors

Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29

Can anyone explain the output : b h hn x

The String arg constructor calls its superclass constructor and that explains the b h in the output. I cannot understand the hn. What about the no arg superclass constructor?

Can anyone just run me through the above example?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Just follow the call path, house(string) calls house() calls building(); The "hn x" should be the easiest to follow as that is via the first call.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Line 19 calls line 12. Line 14 calls line 11. There's an invisible call to "super" at the beginning of House's no-argument constructor, inserted by the compiler; that goes to line 3, which prints "b". Then control returns to line 11, which prints "h". Then control returns to line 15, which prints "hn x". Then control goes back to line 20, and the program exits. The "Building" constructor with a String argument is never used.


[Jess in Action][AskingGoodQuestions]
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Thanks.

So in what condition will the Building String arg constructor will be invoked.
1. if there is a this(); defined in the no arg building constructor?

any other case?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Sanjana Sharma wrote:
So in what condition will the Building String arg constructor will be invoked.


Without changing the Building or House code? Only if somebody used 'new Building("x")' . Generally, you might want House(String) to call Building(String); you'd do that like

Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
So two cases:
1. if we had

2.

or we simply called Building("x");

Correct me if i am wrong.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Sanjana Sharma wrote:

When you say this(); , you are invoking the no arguement constructor of the Building. So just think what will happen when you call the no argument contructor of the Building from the no argument contructor of the Building itself .

Why don't you try compiling it and see the result ...
sreeram koneru
Greenhorn

Joined: Feb 28, 2011
Posts: 2
Sanjana Sharma wrote:
Can anyone explain the output : b h hn x

The String arg constructor calls its superclass constructor and that explains the b h in the output. I cannot understand the hn. What about the no arg superclass constructor?

Can anyone just run me through the above example?


By writing a no argument constructor, you are forcing the child classes to add a constructor with super();
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
I got it. thanks a lot..
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Sanjana Sharma wrote:I got it. thanks a lot..


what? Tell us your current understanding ...
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
I complied the code with different combinations.

if I try to compile : , i will be invoking the same constructor again for which complier throws a error.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Good .... So you have learned two major things from this thread.
1) Recursive invocation of a constructor is not allowed.
2) To try out things yourself before posting a query ..

Now can you think about why recurion is not allowed in constructors?
Rajeev Bhutani
Greenhorn

Joined: Mar 01, 2011
Posts: 1
Ernest Friedman-Hill wrote:Line 19 calls line 12. Line 14 calls line 11. There's an invisible call to "super" at the beginning of House's no-argument constructor, inserted by the compiler; that goes to line 3, which prints "b". Then control returns to line 11, which prints "h". Then control returns to line 15, which prints "hn x". Then control goes back to line 20, and the program exits. The "Building" constructor with a String argument is never used.



Very correct said.I am fully agree with you.
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
Ya very correctly explained.

Prem Sir, we cant have recursive constructor calls , cos constructor invocation marks the creation of an instance. so recursive calls dont make sense.

Correct me if i am wrong and i missed something.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Not "ya" or "cos", please.
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
i apologize for the usage.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Apologies accepted
Sanjana Sharma
Greenhorn

Joined: Jan 08, 2011
Posts: 29
So , was i right on the reasoning for recursive constructor invocation?
if not, correct me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Varun Saini,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Contructors
 
Similar Threads
How exactly Constructor invoke...
Overloaded constructor question
Are this() and super() same?
Superclass Constructor
a extends b , which constructor is called in a?