wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in 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 "doubt in constructor..." Watch "doubt in constructor..." New topic

doubt in constructor...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362

It is giving compilation error as
can not find symbol Base()
In Base class´┐Ż..
Here I am calling long parameterized constructor in Test2l class only..
But why this error?
Can anyone explain me?

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

compiler calls super() inside the Test2l() constructor implicitly. so,it calls Base() which is not there
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
In any derived class if you have a constructor it MUST explicitly call some constructor of the parent class.
If you do not specify then it looks for the default constructor of the Base class.
In your case the default constructor of the Test2 class does not explicitly call the parameterized constructor of the Base class.
Thus the compiler searches for the default which it cannot find and thus the error. Try removing the default constructor or alternatively calling the parameterized constructor from the default. It will work

- Aditya Webservices Blog
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
but here it is invoking parameterized constructor in Test2l i.e Test2l(long)

by using new Test2l(2);

why should it go for default constructor in Test2l?

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
the compiler add super(); implicitly so it search for default constructor in Base class which is not available
thats why it is showing error..

it doesnot matter which constructor we are invoking we must satisfy compile rules...

am i right?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Yes you are right..

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vinay Dinakar

Joined: Dec 30, 2008
Posts: 17
compiler look for default constructor of base class because you used this code .


remove above line and it will compile properly.


~ Vinay Ds
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

"vinay ds", please check your private messages for an important administrative matter.

[Asking smart questions] [About Bear] [Books by Bear]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
If you do not call explicitly super(...) from your constructor, then only compiler will add super().

for your code compiler is adding super() in second constructor.

I agree. Here's the link: http://aspose.com/file-tools
subject: doubt in constructor...
It's not a secret anymore!