output: base default constructor derived constructor:9 derived default constructor could someone explain the output in terms of this() used in the code?
(made not so loud) [ June 21, 2005: Message edited by: Barry Gaunt ]
rajan singh
Greenhorn
Joined: Jun 08, 2005
Posts: 17
posted
0
here "this(9)" is used to invoke one argument construtor from default constructor. [ June 21, 2005: Message edited by: rajan singh ]
Piyush Sam
Greenhorn
Joined: Jun 02, 2005
Posts: 24
posted
0
Hi,
Both your base and derived class have two constructors one defualt and other which takes integer as an argument.
Now have a look at this line Derived obj = new Derived();
Since you are creating a derived class object without giving any arguments and in your dervied defualt constructor you have not called base class defualt constructor so while intializing your base class, your base class's defualt constructor gets called now you have this line:
this((int) 9);
this refers to the current object and in this program our current object is derived object, which consist of base object + derived object and since we have not called super class's constructor Base(int) explictly so defualt constructor gets called, now base part has been initialized
this((int) 9); statement causes Derived(int j) to be invoked and not the base(int) bcoz "this" will invoke the method of the object being created and in our case Dervied class's ojbect is being created thats why Dervied(int) gets called.
Regards,
Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
Rachana Sharma
Greenhorn
Joined: Jun 21, 2005
Posts: 15
posted
0
thanks for the explanation but how do you explain this output class Base{ public Base() { System.out.println("base default constructor"); } public Base(int i) { System.out.println("base constructor:"+i); } }
public class test { public static void main(String args[]) { Derived obj=new Derived(); } } output: base constructor:9 derived constructor9 derived default constructor shouldn't the base class default constructor be called here again in the default derived class constructor?? then ideally the oupt should have been : base default constructor base constructor:9 derived constructor9 dervived default constructor
Piyush Sam
Greenhorn
Joined: Jun 02, 2005
Posts: 24
posted
0
In your derived class defualt constructor you have this((int) 9), that will invoke Derived(int j) and since inside this method you are calling super(j) it causes base class integer arugment constructor base(int) to be invoked, so is your o/p
and in your first code u didnt call any super class constructor explictly so base class defualt contructor got called but here you are calling explictly super(int) it will invoke the appropriate base class constructor.
Regards, [ June 21, 2005: Message edited by: Piyush Sam ]
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
Please use tags to make your formatted code more readable. Thanks