aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
Hi,

I am not able to figure out, why this code is giving a compiler error ?

Thanks .
[ January 07, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Puja" you probably missed my previous post, so here it is again:

"Puja" welcome to JavaRanch. Please read our JavaRanch Naming Policy and change your displayed name to conform with it. We require a <first name> <family name> format, preferably your real names.
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It is also a great help to other posters if you include your formatted code within tags. There is also a special button for this purpose.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The constructor Test() is trying to implicitly call the constructor Base(), but there isn't one. Remember that because you have defined two constructors in Base, the compiler will not provide the constructor Base() automatically. You must define it yourself.
Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
but,new Test(2) will call the one argument constructor which will call the Base's one argument constructor.
Kailas Lovlekar
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Hi Puja,
The problem is you did not specify default constructor for Base class. As soon as you specify your own constructor for a class, java no longer provides default constructor for your class.
In your code - if an object of Test is created, it will try and call default constructor of Base.(Which doesn't exists)
Add - Base(){} to Base class and it should compile
Kailas


SCJP/SCWCD/SCEA/OCA/Pega System Architect
Kailas Lovlekar
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Opps looks like we replied at same time.
Even when you call Test(2)
There is a inbuilt super call to base which is with no arguments.
other way might be to place a super call with int argument in your Test(int)
constructor as first line.
Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
Kailas, you mean to say that Test ( ) will implicitly call the no arg base constructor......

But,
new Test (2) will call the single arg constructor of Test class......no arg of the Test class is not called and so, if no arg constructor of Test class is not called then why is default constructor of base class is being called ?
Kailas Lovlekar
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Kailas, you mean to say that Test ( ) will implicitly call the no arg base constructor......

But,
new Test (2) will call the single arg constructor of Test class......no arg of the Test class is not called and so, if no arg constructor of Test class is not called then why is default constructor of base class is being called ?

When you extend a class, java implicitly calls super() from all the constructors. So from Test, whichever constructor you use, an implicit super() call is placed is used.
This allows initialization of superclass variables.
In other words -

Test(){}
Test(int a){}

is equivalant to Test(){super();}
and Test(int a){super();}

Note that in all constructors it is looking for Base(){}

When you override default costructors, you can no longer rely on this default super() call.

So to fix this you either provide Base(){} constructor or

place a call like this in every constructor -
Test(int a){super(2);// asuming you have Base(int a){}}

Does it make sense now?



amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Thats all fine

but tell me one thing that ...java put default constructor itself...
then why there is neeed for explicitly writing Base(){ } ???


regards,
amit


Thanks and Regards, Amit Taneja
Sulbha Jan
Ranch Hand

Joined: Jan 02, 2005
Posts: 37
Quote: The following quote was posted by Barry Gaunt on January 07, 2005 02:49 AM,in this same thread which explains everything.
The constructor Test() is trying to implicitly call the constructor Base(), but there isn't one. Remember that because you have defined two constructors in Base, the compiler will not provide the constructor Base() automatically. You must define it yourself.

[ January 09, 2005: Message edited by: Sulbha Jan ]
Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
Originally posted by amit taneja:
Thats all fine
but tell me one thing that ...java put default constructor itself...
then why there is neeed for explicitly writing Base(){ } ???
regards,
amit

IMO,when you are defining the constructor explicitly,compiler assumes that you will also write default one.When you don't ,then it will give an error.


MH
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructor