Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in constructor...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net

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?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler calls super() inside the Test2l() constructor implicitly. so,it calls Base() which is not there
 
Aditya Keyal
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right..
 
Vinay Dinakar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler look for default constructor of base class because you used this code .

Test2l()
{
}


remove above line and it will compile properly.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vinay ds", please check your private messages for an important administrative matter.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic