• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt regarding constructors

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic