GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt regarding constructors 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 regarding constructors" Watch "doubt regarding constructors" New topic
Author

doubt regarding constructors

preethi venkatarangan
Greenhorn

Joined: Jan 21, 2007
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
preethi venkatarangan
Greenhorn

Joined: Jan 21, 2007
Posts: 27
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

Joined: Jan 18, 2007
Posts: 25
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.


Kiran
preethi venkatarangan
Greenhorn

Joined: Jan 21, 2007
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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

Joined: Aug 03, 2002
Posts: 7729
As Henry predicted, the code does not compile unless the no-args constructor Base() is provided. I just tried it to prove it.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
preethi venkatarangan
Greenhorn

Joined: Jan 21, 2007
Posts: 27
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

Joined: Jan 18, 2007
Posts: 25
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

Joined: Feb 09, 2007
Posts: 12
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
 
GeeCON Prague 2014
 
subject: doubt regarding constructors