• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding constructors

 
preethi venkatarangan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a base class as follows :


it throws a compile time error as cannot find symbol constructor Base

When i specify super(2) inside Derived constructor it compiles fine.

I thought the new Derived(2) will invoke the constructor with long arg which will invoke the super constructor(in this case Base) with int arg and would display "the base constructor with int arg" but it gives a compile time error as above

Can someone explain how ?

Thanks
Madhavi
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the new Derived(2) will invoke the constructor with long arg which will invoke the super constructor(in this case Base) with int arg and would display "the base constructor with int arg" but it gives a compile time error as above

Can someone explain how ?


Just because your class don't use a constructor doesn't mean that the Java compiler can assume that no other class will.

In the case, the problem is with the other Derived constructor (that takes no parameters). It doesn't specify which Base constructor to use, which means that it will use the no-arg base constructor, which doesn't exist.

Henry
 
preethi venkatarangan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:

In the case, the problem is with the other Derived constructor (that takes no parameters). It doesn't specify which Base constructor to use, which means that it will use the no-arg base constructor, which doesn't exist.

Henry

now suppose i replace the Derived() {} by Derived (String s) { } constructor.the code compile fine ... though Base constructor with String arg does not exist...


can you explain this... Sorry its just that im a bit confused

Thanks
Madhavi
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhavi,

When you replace the Derived() {} by Derived (String s) { } constructor, it still compiles because you are not calling the default (no-args) constructor of the derived class in your code.
 
preethi venkatarangan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kiran Gavate:
Hi Madhavi,

When you replace the Derived() {} by Derived (String s) { } constructor, it still compiles because you are not calling the default (no-args) constructor of the derived class in your code.


thanks i got it...
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kiran Gavate:
Hi Madhavi,

When you replace the Derived() {} by Derived (String s) { } constructor, it still compiles because you are not calling the default (no-args) constructor of the derived class in your code.


Actually, no. The "Derived(String s) {}" constructor will also *not* compile. As it will attempt to call the no-arg Base constructor, which does not exist.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now suppose i replace the Derived() {} by Derived (String s) { } constructor.the code compile fine ... though Base constructor with String arg does not exist...


Another point. When you don't specify which super class constructor to call, it will call the no-arg constructor. It doesn't call the base constructor that matches it's signature -- Derived(String s) does not implicitely call Base(String s), it calls Base(), which doesn't exist.

Henry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry predicted, the code does not compile unless the no-args constructor Base() is provided. I just tried it to prove it.
 
preethi venkatarangan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
As Henry predicted, the code does not compile unless the no-args constructor Base() is provided. I just tried it to prove it.


yeah i too tried it it dint sorry i had by chance included the no-arg constructor in base class because of which it compiled...sorry for the mistake...and thanks for clearing my doubt...


Thanks
Madhavi
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification. I was thinking that it would be a runtime error. good to learn that it will not compile at all.
[ February 09, 2007: Message edited by: Kiran Gavate ]
 
Akbar Upadyayula
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derived(){ } means Derived(){super();}
in code optimisation phase compiler rewrites the code as above mentioned if we don't write the deafualt constructer.
So Derived class constructer looks for super class constructer that exactly match with super(); that is no arg Base class constructer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic