Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but,new Test(2) will call the one argument constructor which will call the Base's one argument constructor.
 
Kailas Lovlekar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kailas Lovlekar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sulbha Jan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1899
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic