Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes constructor called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor called?" Watch "constructor called?" New topic
Author

constructor called?

fan tung
Greenhorn

Joined: Mar 22, 2003
Posts: 5
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.
Amy Phillips
Ranch Hand

Joined: Apr 02, 2003
Posts: 280
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
Jim Toy
Greenhorn

Joined: Oct 17, 2002
Posts: 14
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();
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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
SCJP, MCDBA
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Please reply if you understand it or not
thank you for your attention
fan tung
Greenhorn

Joined: Mar 22, 2003
Posts: 5
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

Joined: Jan 04, 2003
Posts: 867
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

Joined: Jan 04, 2003
Posts: 867
Please reply if you are clear
or I can show somw examples to let you clear about the concept of constructor
 
wood burning stoves
 
subject: constructor called?