hi 1) how is the flow of execution of a program. static variable-->static intializer-->instance variable-->instance initializer-->constructor of superclass-->constructor of subclass Am i right. 2) when a no arg constructor of subclass is called then which constructor of superclass is called. Is it only the no arg constructor or also the matching constructor in superclass is called or no constructor from super class is called.
nss
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
1) how is the flow of execution of a program. static variable-->static intializer-->instance variable-->instance initializer-->constructor of superclass-->constructor of subclass Am i right.
a note, Depends on whichever come first too.
2) when a no arg constructor of subclass is called then which constructor of superclass is called. Is it only the no arg constructor or also the matching constructor in superclass is called or no constructor from super class is called.[/B] if u didn't put super(....) it is the no arg constructor of super class. If no super(...) and this(...) complier will put super() for you.
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
"instance variable-->instance initializer-->constructor of superclass" it's the other way round, remember the superclass instances should be initialized before subclasses instances. hope helps
SCJP2. Please Indent your code using UBB Code
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
class Superclass { Superclass() {System.out.println("1"); } Superclass(int a) {S.o.p("2"); } } class Subclass extends Superclass{ Subclass() {S.o.p("3"); } Subclass(int b) {S.o.p("4"); } public static void main(String args[]) { Subclass s= new Subclass(5);} } what will the output be? Will it be 4
Adrian Muscalu
Ranch Hand
Joined: May 08, 2000
Posts: 73
posted
0
In my oppinion, the output will be 1 and 4. The constructor in the subclass will call the "no-parameter" constructor from the superclass and then it will take care of himself. Correct me if I'm wrong.
SCJP2, SCWCD
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
Got it but just to confirm once again the constructor of the subclass will not call its no arg costructor but only the no args constructor of super class and if the superclass has no args constructor then will print only 4. correct me if i am wrong thanx in advance neha