Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How exactly Constructor invoke...

 
Greenhorn
Posts: 17
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends ....

I have a doubt regarding constructor call ..( this prog. is from KB book)


class Building{

Building(){System.out.println("b ");}

Building(String name){

this();
System.out.println("bn "+name);
}
}

class House_2 extends Building{


House_2() { System.out.println(" h ");}

House_2(String name){

this();
System.out.println(" hn "+name);
}

public static void main(String[] args){

new House_2("x ");
}
}


Ans is : b h hn x

i thought the answer will be --- b bn h hn x
why 'hn' not printed?
Please Help!
Thanks!!
 
Vishu Patil
Greenhorn
Posts: 17
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its quite simple

new House_2("x "); it will call the String argument constructor

it will call this(); & System.out.println(" hn "+name); on stack

the default constructor will call super(); with System.out.println(" h ");System.out.println(" hn "+name); on stack

the no arg constructor of super class will print "b "

1st entry of stack System.out.println(" h "); will print " h "

2nd entry of stack System.out.println(" hn "+name); will print "hnx"

output: b h hnx

please paste code within code tag

avi
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Patil wrote:
why 'hn' not printed?



there is nothing shocking buddy in it. the compiler inserts a default super constructor in a subclass constructor.

here super(); will be inserted in the first line of default constructor of house_2

a constructor can never have both calls to this(); & super(); hence compiler will not insert anything in the String arg constructor

avi
 
Vishu Patil
Greenhorn
Posts: 17
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks avi... ..now my doubt is clear....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic