Win a copy of Microservices Testing (Live Project) 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

constructor called?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,i had this beginner's constructor question (which constructor is executed first). this is my codes:
// create a super class
class A {
A() {
System.out.println ("Constructing A.");
}
}
// create a subclass by extending class A
class B extends A{
B() {
System.out.println ("Constructing B.");
}
}

// create a subclass by extending B
class C extends B {
C() {
System.out.println ("Constructing C.");
}
}
class OrderOfConstruction {
public static void main (String args[]) {
C c = new C();
}
}
and i got this output:
Constructing A.
Constructing B.
Constructing C.
but if i changed my codes to:
// create a super class
class A {
A() {
System.out.println ("Constructing A. ");
}
}
// create a subclass by extending C
class B extends C {
B() {
System.out.println ("Constructing B.");
}
}

// create a subclass by extending A
class C extends A {
C(){
System.out.println ("Constructing C.");
}
}
class OrderOfConstruction {
public static void main (String args[]) {
C c = new C();
}
}
and i got this output:

Constructing A.
Constructing C.

why "Constructing B." is not showing? do i have
to put in order (A, B, C) not (A, C, B)?
Please help me. Thanks.
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Believe its because you can't extend Class C until you have actually created it i.e. in this case after you try and create B
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the order of the objects in your first example it was A->B->C in your second it is A->C->B.
Therefore when you created C it did not have to "go through" B.
To get what I think you are looking for you would change your object instantiation code to
C c = new B();
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi fan
yes!
the first programme result is
Constructing A
Constructing B
Constructing C
class OrderOfConstruction {
public static void main (String args[]) {
C c = new C();(*)
}
}
(*)firstly, in the main method create the c object that invoke the c() constructor so
class C extends B {
C() {
(1)super();
System.out.println ("Constructing C.");
}
}
But you should notice that (1) is invisible by default. so,it is invoke B() constructor so
class B extends A{
B() {
(2)super();
System.out.println ("Constructing B.");
}
}
At the same concept (2) is invisible by default
so,it is invoke A() constructor so
class A {
A() {
System.out.println ("Constructing A.");
}
}
Finally, the constructor A() without superclass
so the first print
Constructing A
then
Constructing B
finally
Constructing C
the order is (1),(2),(3)
Notice that the super(), must be the first statement inside the constructor
Next question is following
class OrderOfConstruction {
public static void main (String args[]) {
C c = new C();
}
}
So as the same thing
it invokes the C() constructor
class C extends A {
C(){
(1)super();
System.out.println ("Constructing C.");
}
}
as the same thing but notice that the superclass of C is A

so
class A {
A() {
System.out.println ("Constructing A. ");
}
}
but now A without superclass
So, it will print
Constructing A
then
Constructing C
because the programme without involves the constructor B(), so the print out without
Constructing B
I hope that I can answer your question clear
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reply if you understand it or not
thank you for your attention
 
fan tung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, siu,
i knew the 1st part but i don't understand is the second part. thank you walked me thru and it help me a lot.
thank you so much,
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok let me explain detail with the question 2
class OrderOfConstruction {
public static void main (String args[]) {
C c = new C();
}
}
it invokes the C() constructor
class C extends A {
C(){
(1)super(); //invoke constructor A()
System.out.println ("Constructing C.");
}
}
as the same thing but notice that the superclass of C is A so that A() constructor invoked

so
class A {
A() {
System.out.println ("Constructing A. ");
}
}
but now A without superclass
So, it will print
Constructing A
then
Constructing C
because the programme without involves the constructor B(), so the print out without
Constructing B()
and also B is subclass of class C,
when invokes the C() constructor
it would not invoke the B() constructor
you notice that
class C extends A {
C(){
/*invoke the super class only by default
and without invoke subclass(*)
(1)super();
System.out.println ("Constructing C.");
}
}
(*)
class B extends C {
B() {
System.out.println ("Constructing B.");
}
}
the flow is that first invoke the constructor C()
-->invoke super()-->constructor A()-->print statement of A-->print statement of C
I hope that it may be clear now
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reply if you are clear
or I can show somw examples to let you clear about the concept of constructor
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic