File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Contructors

 
Sanjana Sharma
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanjana Sharma
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So two cases:
1. if we had

2.

or we simply called Building("x");

Correct me if i am wrong.
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. thanks a lot..
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjana Sharma wrote:I got it. thanks a lot..


what? Tell us your current understanding ...
 
Sanjana Sharma
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Java Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not "ya" or "cos", please.
 
Sanjana Sharma
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i apologize for the usage.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
 
Sanjana Sharma
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So , was i right on the reasoning for recursive constructor invocation?
if not, correct me
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varun Saini,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic