File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need explanation" Watch "need explanation" New topic
Author

need explanation

Rachana Sharma
Greenhorn

Joined: Jun 21, 2005
Posts: 15

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
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
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
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);
}
}

class Derived extends Base{
Derived()
{
this((int)9);
System.out.println("derived default constructor");
}
Derived(int j)
{
super(j);
System.out.println("derived constructor"+j);
}

}

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
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

System.out.println("base constructor:" + i);

then your

System.out.println("derived constructor" + j);

and lastly

System.out.println("derived default constructor");

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
Please use tags to make your formatted code more readable.
Thanks


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rachana Sharma
Greenhorn

Joined: Jun 21, 2005
Posts: 15
thanks for the answer it seems to be clear for me now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need explanation
 
Similar Threads
Constructors
inheritance question
about constructors
Constructor doubt
A simple Q but still have doubt